I created a progress dialog into a fragment by using this effect. Now i must call it in different activities. When I call the effect the buttons at the background must not work. How can I do that?
First i need to learn how to call the fragment in a different activity. Then i must make the buttons at the background unclickable but there are many of them so ‘setclickabla(false)’ would be a tiring choice.
Progress Dialog Fragment XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ProgressDialogFragment">
<com.skyfishjy.library.RippleBackground
android:id="#+id/ProgressDialogs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CB000000"
app:rb_color="#80FFFFFF"
app:rb_duration="2500"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_scale="6">
<ImageView
android:id="#+id/centerImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/logo" />
</com.skyfishjy.library.RippleBackground>
ProgressDialogFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
rippleBackground.startRippleAnimation();
return view;
}
For the buttons at the background to respond to clicks, you have to implement the onClick listener of the various buttons.
<com.skyfishjy.library.RippleBackground
android:id="#+id/ProgressDialogs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CB000000"
app:rb_color="#80FFFFFF"
app:rb_duration="2500"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_scale="6">
<Button
android:id="#+id/centerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/logo" />
In your layout above, you are making use of ImageView which I changed to centerButton and you can make it respond to clicks this way:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
rippleBackground.startRippleAnimation();
Button button=(Button)view.findViewById(R.id.centerButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// your respond to clicks
}
});
return view;
}
Call your fragment from your activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new MainFragment(); // your fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
Related
the following is my java file for one of the fragments that am using to have multiple image buttons which open multiple weebpages.
please help.
treat me as a noob.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.menu8_layout, container, false);
return rootview;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button bt1 = (Button) findViewById(R.id.btn_click_login);
btn_login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://google.com"));
startActivity(myWebLink);
});
}
}
here is the related xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt1"
android:clickable="true"
android:autoLink="web"
android:cursorVisible="true"
android:linksClickable="true"
android:onClick="goToSo"
android:text="some" />
I have an Activity with a button like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClickListener"
android:text="Show Fragment" />
</RelativeLayout>
And my onButtonClickListener method from xml is(here I create the DialogFragment and set onClickListener for a clear button from dialog):
public void onButtonClickListener(View view) {
FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");
final EditText messageText = (EditText) findViewById(R.id.editText);
Button clearButton = (Button) findViewById(R.id.button_clear);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
messageText.setText(null);
}
});
}
My problem is that when DialogFragment shows it throws NullPointerException:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.cristi.dialogfragmenttest2.MainActivity.onButtonClickListener(MainActivity.java:51)
At this line of code: clearButton.setOnClickListener(new View.OnClickListener() {
My xml for DialogFragment is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLength="200" />
<Button
android:id="#+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
What could be the problem with the setOnClickListener? Why does it says that my button from DialogFragment is null?
Any reference or advice is welcome! Thanks in advance!
Edit:
-MyDialogFragment code:
public class MyDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("Enter message");
return rootView;
}
You must have refernece to the root view where is your button button_clear, then call rootView.findViewById(...) to get the button reference...
Solution:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("Enter message");
Button clearButton = (Button) rootView.findViewById(R.id.button_clear);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
messageText.setText(null);
}
});
return rootView;
}
I have an base Fragment with an FrameLayout and I want to use this Layout in the derived Fragment and add an onTouchListener to it.
BaseFragment.java
public class BaseFragment extends Fragment {
protected FrameLayout baseFrameLayout;
protected View baseView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
baseView = inflater.inflate(R.layout.fragment_base, container, false);
baseFrameLayout = (FrameLayout) baseView.findViewById(R.id.frameLayout);
return baseView;
}
}
fragment_base.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
DerivedFragment.java
public class DerivedFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
baseFrameLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// do something
return true;
}
});
return baseView;
}
}
Unfortunately the onTouchListener is not executed when I touch the FrameLayout
EDIT:
Finally I add the DerivedFragment to another Fragment in this way:
OverViewFragment.java
public class OverViewFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_overView, container, false);
Fragment derivedFragment = new DerivedFragment();
FragmentTransaction devTrans = getChildFragmentManager().beginTransaction();
devTrans.add(R.id.derivedFragmentId, derivedFragment).commit();
Fragment footerFragment = new FooterFragment();
FragmentTransaction statTrans = getChildFragmentManager().beginTransaction();
statTrans.add(R.id.footerFragmentId, footerFragment).commit();
return view;
}
}
fragment_overView.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/derivedFragmentId"
android:layout_above="#+id/footerFragmentId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:gravity="center"
/>
<FrameLayout
android:id="#+id/footerFragmentId"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I had never used empty Layouts before. So i just tested it.
With the following lines the Layout is not visible and does not get clicks:
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
The following lines work:
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
So either make the Layouts match_parent or add views inside.
Solved! Thanks everyone for your help.
I am struggling with Fragments as a concept, and especially seem stuck on this one thing. What I'm trying to do is, using my fragment, manipulate its own layout's data. Every time I try to access an ImageButton from within the Fragment, it crashes the application. It works fine from the activity. Am I just misunderstanding Fragments fundamentally?
Code(cut down for size)-
This is the beginning of the activity my fragment is called from:
Display.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragmentClass MyFragment = new MyFragmentClass();
fragmentTransaction.add(R.id.fragment_container, MyFragment);
fragmentTransaction.commit();
The XML for that Activity:
activity_display.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.mycompanyname.myprojectname.Display"
android:id="#+id/display_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</RelativeLayout>
The Fragment
MyFragmentClass.java
public class MyFragmentClass extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout_screen, container, false);;
}
public void MethodTest()
{
}
}
The Fragment's XML file:
fragment_layout_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_layout_screen"
tools:context="com.mycompanyname.myprojectname.MyFragmentClass">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_button"
android:clickable="true"
android:onClick="buttonPress"
android:src="#drawable/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Basically I had all of this originally in Display.java, but wanted to add fragments, so I'm trying to move stuff out and into fragments, but I still need the ability to manipulate the xml info, I just can't.
From inside the Display.java activity I can easily call the ImageButton like this:
ImageButton myButton = (ImageButton) findViewById(R.id.my_button)
but if I do the same from MethodTest in the fragment, the app crashes.
I've searched many suggestions on here, trying various solutions from here: findViewById in Fragment but none of those seemed to work.
I've been reading through this for the setup: http://developer.android.com/guide/components/fragments.html and can't seem to find what I'm doing wrong.
Any help would be appreciated, and if you have any questions about my setup, please ask.
declare on fragment private View rootView;
On the onCreateView(...) set rootView = inflater.Inflate(..); and then access the imagebutton as ImageButton mButton =(ImageButton) rootView.findViewById(..);
Do like this in fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview, container, false);
GridView gridview = (GridView) view.findViewById(R.id.grid);
return view ;
I'm writing and android 4.4 project using android studio.
I'm new to the fragments idea and trying to create a simple application with a button that the click handler sends a message to the log.
this is the fragment class
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
return rootView;
}
public void addStringClickHandler(View v) {
Log.d("tag","hello");
}
}
this is the fragment layout XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.tuxin.myalcoholist.myalcoholist.myalcoholist.MainMenuActivity$PlaceholderFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_drink"
android:id="#+id/add_drink"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:onClick="addStringClickHandler" />
</RelativeLayout>
as you can see i set in the XML android:onclick to addStringClickHandler
and in the fragment class i created that function, but when I execute the application
I get an error that the runtime could not find a method addStringClickHandler(view)
what am I missing?
Your android:onClick handler method has to belong to the Activity hosting your fragment. Just move addStringClickHandler() method to the activity.
If you want to have a listener method in the fragment you have to set listener in the code like this.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("tag","hello");
}
});
return rootView;
}
Try to register the onClick method when inflating the view.
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do what you need
}
});
return rootView;
Or you can set the fragment to be the on click listener
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(this);
return rootView;
and then make the fragment implement OnClickListener.