Jetpack navigation: Change toolbar buttons per fragment - android

After calling NavigationUI.setupActionBarWithNavController, the title of the toolbar automatically changes to the android:label of the fragment defined in the navigation graph. But what if I want to show different buttons on the right side of the toolbar depending on the fragment? I used to create separate activities so I did not have to change the toolbar buttons dynamically, but now we use multiple logical screens (fragments) within one Activity, the buttons need to be changed just like the title.
For example, suppose my navigation has two fragments, f1 and f2. I want to show an "About" button for f1, and "Save" for f2. Is there an automatic way as the title, or do I have to manually do that in the Activity by using such methods as in (How can I dynamically create menu items?)?

Related

Correct way to manage showing multiple custom toolbars?

The structure of the app is one Activity and multiple Fragments(+ 20).
The Activity has a Toolbar with a title and back button that i used in multiple Fragments.
Then there is two more types of Toolbars that i use inside multiple Fragments, one contains an EditText and the other one is an Expandable Toolbar.
An example would be:
Fragment A: Uses Toolbar from Activity. Show that one, hide if another is showing.
Fragment B: Has a Toolbar of it own, now i need to hide the Activity toolbar.
What would be the correct approach to show/ hide the correct toolbar?
Things i tried:
Using onDestinationChangedListener checking the id of the fragment and show/ hide the correct toolbar. The problem here is that it cause the screen to flicker and i have to check for every id.
toolbar.isVisible = destination.id in listOf(id1, id2...)
Hide or show the correct Toolbar from the Fragment. But then i need to set the toolbar on every Fragment.
(requireActivity() as MainActivity).showToolbar(true)
Removing the Activity Toolbar and creating a new one inside every Fragment. But i don't know if the best way and if it is expensive to constantly inflate the Toolbar.

Trying to create a app that has 3 navigation buttons at the buttom and a new activity comes up when corresponding button is

I'm trying to create a app that navigates in the same way movie pass app does. At the button it has 3 buttons that stay there and a new Activty comes up when you press the buttons.
I could put these buttons on all 3 activtes, but then if I make a code change to the buttons it must be changed in three places.
Two options could help with this. 1) Use a fragment for the content shown above the three buttons so you just have a single activity, and the buttons change which fragment is shown. 2) If you are using a BottomNavigationView or some other view that takes a listener you can create your own OnNavigationItemSelectedListener implmentation that handles all the logic for what happens on button presses. Then that code is in a single place, and you can use that listener in all three activities.
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(new MyNavigationListener());
Use fragments with bottom navigation view it will sortout your problem.
if you use activities instead of fragments then you need to create bottom navigation view for each activity. it will increase your code length & complexity.
If you don't have idea how to use fragments with bottom navigation view. Please follow below link.
https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/
I hope it will help you :)

Is it possible to implement a TabLayout with activities

I have a Tablayout kind of a bottom navigation, and I want that when the user clicks in a cardview that I have it sends him to another Tablayout but when that happens the bottom navigation does not appear anymore. That is only possible with activities right?
A TabLayout is just a view like any other. It can be inflated as a part of another view, or as the content view for a Fragment or an Activity. So, you're not restricted to using just an Activity with a TabLayout.
Now, if you want it so that when you click on a CardView you're taking to another screen without a TabLayout, you have a couple of options:
Open a new activity
Since this second Activity is going to have a different content view, it's not going to have the TabLayout from the first activity.
Hide the TabLayout and change the content surrounding the CardView
Since we're sticking with the first Activity, we're going to need to make all of our content changes to the views in the Activity. This means setting the visibility on the TabLayout to View.GONE and potentially lots of changes to the rest of the views depending upon what your layout contains.
I noticed that you didn't mention a ViewPager at all. Typically, this is what will be used with a TabLayout to swap between Fragments when you click on each tab. You could have all of your tabs' content in separate Fragments and then when you click on a CardView, just swap that tab's Fragment out for a different one and hide the TabLayout.
So, to answer your question, it is easier to just open a new activity, but it is possible to not use a second activity if you want to put in the work to modify your first one in runtime.

Changing navigation drawer layouts

How to inflate new layout on custom navigation drawer when, for example, I click another button in main activity? Is that possible? I havent found any function like beforeOpenDrawer or etc. Or should I construct 4 seperate drawers?
Thank you for your answers
We managed that using fragments. Our DrawerLayout contains a FrameLayout in which we put a "menu" fragment. Some clicks replaces the "menu" fragment by a "submenu" fragment.
I'm not sure I get what you mean.. but if what you want is:
. To create a navigation drawer with custom layout (not just list view) then it is possible.
. Or if you having multiple buttons or radio buttons inside the drawer layout and you need for example to listen for their onClick events … you only write the code once inside the main activity that inflate the left or right drawer.. I've tried something like that but I was using fragments not activities.

Activity with variable content

I need to create an activity with an ActionBar, so that if you press the action bar buttons, the contents of the activity display the same information in different layout (one is a pie chart and the other a listview).
I don't want to use tabs or a viewpager, so what would be the best way to do this?
Build each view in a separate fragment, and use the actionbar buttons to set the current visible fragment.

Categories

Resources