Syntax for image button - java

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

Related

Call Main activity from a Fragment

fragment class
Home/main activity
How to limit my main activity to not go back on the last page of my Fragment once return from the last page of Fragment to Main activity like I have One Home activity and that home activity leads to 3 more activities on clicking the button and in which one activity has fragments. The last page of the fragment is leading to home activity but when I back pressed on the home activity it again leads me to the last page of a fragment from where I've come and then I press the back button again then my application shuts
When you are adding your Fragments to the FragmentManager make sure to avoid adding it to the backstack, if you don't want it to appear on its own.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_layout,fragment);
//Adding null will stop the fragment from reappearing on a backpress.
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
In the future please post code in text format. Do not post images of code. If you need to show some view behavior on the screen then its fine to post images, but not code.

How to call a Button from a different fragment (Java)?

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.}

PopBackStack previous last fragment and check which fragment it is

I got a few fragments in MainActivity which can be selected from Navigation Drawer. Now, whenever users press a profile button, it will jumps to UserProfile Fragment. If home button is pressed, it will pop back the last fragment. Since I've assigned each of the fragments a specific backstack name i.e .addToBackStack("abc"), how can I check what is the last fragment by using popBackStack() method ?
To get the last fragment:
FragmentManager fm = getSupportFragmentManager();
int lastFragEntry = fm.getBackStackEntryCount()-1;
String lastFragTag = fm.getBackStackEntryAt(lastFragEntry).getName();
Log.i("Last Fragment Tag->", lastFragTag);
NB: If you want to get the name/tag of last fragment, you also have to use the same Tag during fragment transaction:
ft.replace(android.R.id.container, fragment, "abc");
ft.addToBackStack("abc");
Hope this helps.

Unable to start a fragment from a fragment

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()

Add view to Fragment programmatically Android

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.

Categories

Resources