Add view to Fragment programmatically Android - java

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.

Related

how Set OptionsMenu when certain fragment is shown

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

add toolbar menu only at specific fragments

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

Syntax for image button

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

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

Can't access to a spinner in Java

I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.

Categories

Resources