Hiding of a ToolBar icon only for a specific Fragment - android

I am using an Activity which has 3 fragment.Back Arrow is visible because of using the following line.
android:parentActivityName=".Activity.MainActivity". But I need to hide this <--(Back Arrow) only for a particular fragment abd it has to be visible in another 2 fragment. What do I need to do?

Use this line of code in your fragment onCreateView() where you want to remove the back button
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
or
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);

try this code in your fragment:
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);

Related

How to show action bar when replace a fragment after hiding it on previous fragment?

I have an actionbar on MainActivity level (mainActionBar) and an actionbar on fragment level (fragActionBar). I have 3 fragments (A,B,C). I want mainActionBar to be shown when I'm accessing fragment A and B. On the other hand, I want fragActionBar to be shown when I'm accessing fragment C.
I use replace() when changing between fragments and use addToBackStack to save previous fragment so I can use popBackStack to back to previous fragment. The flow is like this : A<->B <->C. fragActionBar contains up button and if I press the up button it will back to fragment B. mainActionBar doesn't have up button, I use button outside toolbar/actionbar to change fragments between A<->B->C. So only C->B that is using up button.
I have succeeded to show mainActionBar when I'm on fragment A and B. I also have succeeded to show fragActionBar when I'm on fragment C. But when I go back to fragment B, the mainActionBar didn't show up. I try to put supportActionBar?.show() inside onResume(), but it didn't works. I also try to put (activity as AppCompatActivity).supportActionBar?.show() inside setNavigationOnClickListener, still didn't works.
How can I show the mainActionBar when I get back to fragment B after visiting fragment C?
Turns out, I need to use the toolbar I declared in MainActivity by passing the binding from MainActivity to other fragment. It is because I use a different toolbar in other fragment, that's why it won't affect the changes even after I use .show().

Hiding a BottomNavigationView during a fragment transition

I am creating an app using single Activity so on main_activity I set the fragment as NavHostFragment and on the bottom the BottomNavigationView.
In fragment A (first tab selected), If I click on button_1...navigate on a new fragment (A1). Of course inside fragment A1 I need to hide BottomNavigationView so on main_activity I am using addOnDestinationChangedListener to hide BottomNavigationView.
The problem is that if I want use a transition (enter, exit, popup_enter, popup_exit) the BottomNavigationView hide immediatly. (example 1 on image)
How can I do to have the same as example 2 on image?
It looks like you have your 'NavHostFragment' below the 'BottomNavigationView'. So even if you were to delay hiding the BottomNavigationView the fragment transition would still happen behind it. So without changing your implementation completely it won't be possible to have the new fragment transition above the bottom BottomNavigationView.

How to use setSupportActionBar in Fragment without affecting Acitivity Toolbar

while having two Toolbars one for activity and another for Fragment
if I set setSupportActionBar for the Fragment toolbar it means I am loosing SupportActionBar for Activity toolbar Documentation. But is it possible to setSupportActionBar for fragment and Activity both toolbars parallel without affecting each other operating in their own scope . Thanks
No you can't do that, only a Activity has access to setSupportActionBar.
If you want to set it from your fragment use ((Activity)context).setSupportActionBar();
Is your goal just changing the Title and onClick on the menu items for a Toolbar?
Edit:
Try making a toolbar object and calling
toolbar.setMenu();
and then to listen to button clicks use
toolbar.setOnMenuItemClickListener();

Swap toolbar on fragment launch

I have an activity with a NavigationDrawer and a full screen fragment. Clicking on various items in the NavigationDrawer inflates a different fragment in the activity.
I would like to also swap in a different toolbar when launching a different fragment. The reason I want to do that instead of inflating a new menu is that the toolbar I want to swap in is a bit complicated and has things like an EditText in it.
Is there some way to do this either in the activity before inflating a fragment or maybe in the fragment?
Just include your Fragment specific toolbar in your Fragment's layout so that it's visible in your Fragment permanently.
Now in the onStart() method of your Fragment -
getActivity().getSupportActionBar().hide();
This will hide the activity's default toolbar so that it does not overlap with the Fragment's toolbar.
Then again in the onStop() method of your Fragment -
getActivity().getSupportActionBar().show();
So that the Activity's toolbar is visible again outside that Fragment.
each time you call a fragment use "invalidateOptionsMenu" and using fragment id you can set visibility of menuitems in toolbar... if that is what u need... hope it helps

Navigation drawer title according to the Drawer item

I have used getSupportActionBar().setTitle("drawer_title"); and the title changes according to drawer item. But when I go back the title does not get updated. for eg: If I'm at dashboard-fragment toolbar-title is dashboard and after that when I go to message-fragment toolbar-title changes to message, but, after when I press back button it goes back to dashboard-fragment but the title does not updated to dashboard it remains message.
How could I update the title again??
In your fragment use requireActivity() to get the activity to the activitiy's SupportActionBar.
So, to set the title in the fragment then use:
((AppCompatActivity) requireActivity()).getSupportActionBar().setTitle("");
Add following line in onViewCreated method of your fragment:
getSupportActionBar().setTitle("Your title here");
Use getSupportActionBar().setTitle("drawer_title"); inside onResume() method of your fragment.
This works for me I just used getActivity().setTitle("fragment_title"); inside onCreateView() method of fragment.

Categories

Resources