I have two fragments: Fragment1 and Fragment2. In Fragment1 there is a button button and in Fragment2 there is an ImageView ImageView. If the user clicks button in Fragment1 he gets directed to Fragment2, where he can click ImageView. The function function for that is stored in Fragment1.
How can I use an OnClickListener on the ImageView of Fragment2 in Fragment1, even though it isn't in the layout file of Fragment1?
Right now I get the error code:
Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.bottles.Fragment1.onClick(Fragment1.java:36)
You cant do it,if it is not in layout file.
Add that imageview to layout and call it with unique id.
For example id of the imageview is "imageview".
Do this in java file inside of,which extends your fragment:
Imageview imageview;
then in your onCreateView,call this:
imageview=view.findViewById(R.id.imageview);
To make it as a button,do this:
imageview.setOnclicklistner(new OnclickListner){//do what you want here.}
Related
In my main Activity, I have a fragmentContainerView in which I am changing the fragments by using bottomNavigationbar which has 2 navigation Items (home & profile).
Now I want to show OptionsMenu the "profile" fragment is shown and then again remove it when the "home" fragment is shown.
How can I do it? or is there an alternative?
Create a public method in the activity to change set the option menu and call it from the fragment like below.
HomeActivity homeActivity=(HomeActivity) getActivity();
assert homeActivity!=null;
homeActivity.yourPublicMethod();
So I have this app and in the toolbar, there is a drop-down menu and I am looking for a solution on how to show only specific items at specific fragments.
I know that I have to make a java class and link it to the menu, but how do I find out what fragment I am on?
You can move the implementation of setting the toolbar for your fragments in the activity that launches these fragments. In that case, you may have the toolbar in your activity and the activity should have a fragment container where you will put your fragment. Let us consider a layout like the following for your activity.
<RelativeLayout>
<!-- Your toolbar here in the activity -->
<ToolBar>
android:id="+#id/toolbar"
...
</ToolBar>
<!-- Your fragment container here -->
<FrameLayout>
android:id="+#id/fragment_container"
...
android:layout_below="#id/toolbar"
</FrameLayout>
</RelativeLayout>
Now, you have the ToolBar here in your activity and hence, you can set the toolbar when you are loading the fragments in the fragment container. Your activity might have the following functions.
public void launchFrag1() {
// replace the fragment 1 in the fragment container
loadToolbarForFrag1();
}
public void launchFrag2() {
// replace the fragment 2 in the fragment container
loadToolbarForFrag2();
}
Now call the specific function of your activity to load the fragment and the toolbar dynamically as well.
If you are trying to call the method from the fragment, you can always call those methods declared in your activity like the following.
((YourActivity) getActivity()).launchFrag2();
I hope you get the idea.
Please note that this is just a basic implementation of how you can get your dynamic toolbar working in your app. However, your implementation might vary based on the use case that you have.
I hope that helps!
I have an ImageView in the toolbar which I hide as follows:
// Show image in fragment 1
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.VISIBLE);
// hide image in fragment 2
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.GONE);
Try these codes in onCreateView after inflating your layout
How do I make the image button change to different fragment on click?
On the homepage fragment I have an image button. How do I code it so that it changes to a different fragment when clicked. Note that I have java classes for all my fragments as well as the main activity class.
Where would I put the code for this image button and how would I do it because everything I have tried doesn't work.
You can create onClickListener for the image button in home fragment and inside onclick:
Fragment fragment = new {your-fragment()};
FragmentManager fm1 = getActivity().getSupportFragmentManager();
FragmentTransaction ft1 = fm1.beginTransaction();
fm1.popBackStack();
ft1.replace({layout}, fragment).addToBackStack("tag");
ft1.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft1.commit();
Hope this helps
Hello everyone i have a view pager named "viewpager" and two fragments named "Add_Create" and "Add_Create_2".I am using Tab layout to show different tabs.Now in one of the tabs there is "Add_Create" fragment.In Add_Create fragment there is a button and on that button click i want to show Add_Create_2 fragment.Below is my code that i have tried
Add_Create_2 add= new Add_Create_2();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.viewpager,add);
transaction.addToBackStack(null);
transaction.commit();
Now this code is present in first fragment named "Add_Create".
The problem could be at the viewPager. What kind of element is this? If it is a ViewPager, you cannot load it a Fragment. You should to create on it maybe a RelativeLayout, and it will be your fragment container.
Instead of using getFragmentManager(), use getSupportFragmentManager(). Since you're calling it from a fragment, maybe getActivity().getSupportFragmentManager()
In my fragment1 I add view like this:
CheckBox chb = (CheckBox)new CheckBox(getActivity());
linearLayout.addView(chb);
When I load fragment2 to activity's container and then return back to fragment1, there is no added view.
Thanks for help.