Navigation view example
In reference to the above example of NavigationView, say I have "Go to Second item" button in First item layout. If I click on "Go to Second item" button, I am successfully able to go to the Second item layout, but the checked item in NavigationView remains in First item. How do I change the checked state of a MenuItem from another class which extends Fragment. If I directly click on Second item from the Menu, then the checked state changes accordingly, but how to change checked state from another class which extends Fragment.
NavigationView tutorial: Tutorial and Android documentation.
Wrap your menu items with the tag group with attribute android:checkableBehavior="single"
Call NavigationView.setCheckedItem() when you click the button.
Related
I have bottomNavigation in my android app as shown below in picture. It works fine if I just click on home, cart and other bottomNavigation Items.
As you can see in picture that user can navigate to all categories in home. If I navigate to AllCategoriesFragment then in home item will be selected in bottomNavigation which is correct behavior. But if I go to cart and then click again on home then it shows AllCategoriesFragment which is also correct behavior but in BottomNavigation it doesnt highlight the home items, instead it keep cart item highlighted.
How I can get rid of this issue? Or how I can highlight the bottomNavigation selected item programmatically?
I did it programmatically like by getting activity reference inside fragment andd then getting navView from it and afterwards set the check state of the item to true
activity?.let {
val navView = it.findViewById<BottomNavigationView>(R.id.nav_view)
navView.menu.getItem(0).setChecked(true)
}
I would like to click on an item of a listview to show another listview of other items, what can I do?
in my application I have a main Activity, where I have inserted a bottom view navigation, when you click on an item in the bottom view menu, the corresponding fragment appears in the main activity.
my listview is in the first fragment, and in the same fragment the next screen must appear, after the click in the listview.
do I have to replace the fragment of the listview with another one or can I replace only the listview? how?
Thanks for the attention
Instead of those options (replacing the fragment / list view), you could use the onItemSelected method to repopulate the list view object with the new list. Since you already know how to do this, it would seem the simplest option. A simple switch statement would suffice.
I have a navigation drawer in my app. Every Entry in the list of the NavigationDrawer represents one navigation graph. I want to mark the element in which the user currently is.
I managed to get the item to be marked after it is selected and until the visible fragment changes (even if the new fragment is still in the same navGraph). I did that by setting a NavigationItemSelectedListener using NavigationView.setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener). Inside this listeners onNavigationItemSelected(item: MenuItem): Boolean function, I do: item.isChecked = true.
The problem with this is that the item is unchecked after the app starts and the item looses it's checked state as soon as the fragment changes (even if the navGraph doesn't change). How do I do this correctly?
I have also tried to set android:checked="true" in the xml declaration of the menu item I want to be checked when the app starts. It doesn't seem to do anything.
I have also tried to set navView.setCheckedItem(R.id.main_nav_option_one) in the Activity's onCreate function. Again, it doesn't seem to do anything.
Can you try
navView.getMenu().getItem(index).setChecked(true)
in the fragment's onResume?
I managed to solve my problem.
The most important change I had to make is mentioned here. I had to give my menu items the same ID as the ID of the navGraphs. When I did this, I removed my NavigationItemSelectedListener because it was interfering with the automatically generated navigation. Without a NavigationItemSelectedListener I could no longer handle a navigation item being clicked, that doesn't have a corresponding element in the navGraph. However I was able to fix it, by adding the NavigationItemSelectedListener back in, and making the correct calls to NavController.navigate() for every element in the list.
TL;DR:
I had to set the menu item IDs to be the same as my navGraph IDs and make the navigation calls myself anyway in the NavigationItemSelectedListener.
I have an app with BottomNavigationView, which has 2 menu items. Both have their Fragments. On the first item in the Fragment I have a RecyclerView. I want to open the second menu item on click on RecyclerView row and also send some parameters. How and where should I do it?
What am I doing now:
In the Fragment, which I show on the first menu item, I have a method, that I pass to RecyclerViewAdapter, so that it can be triggered each time a row is tapped. But I cannot find any method in my listener as BottomNavActivity to open it's second menu item
You need a reference to your activity within the adapter. You can pass this in during initialisation using
SomeAdapter(var activity: Activity)
Then inside your adapter you set the onClickListener with the following action
activity?.navigation?.selectedItemId = R.id.navigation_id_here
From API 25.3.0 it was introduced the method setSelectedItemId(int id) which let's you mark an item as selected as if it was tapped.
From docs:
Set the selected menu item ID. This behaves the same as tapping on an
item.
Code example:
BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setSelectedItemId(R.id.my_menu_item_id);
For those, who still use SupportLibrary < 25.3.0
View view = bottomNavigationView.findViewById(R.id.menu_action_item);
view.performClick();
I have a checkbox for one of the items in my navigation drawer. On checking it I am setting a preference value to true. In one of my fragments I have a checkbox which is set based on this preference value. Everything works fine except for one case.
That is, when I am on this particular fragment. I open the navigation drawer and check/uncheck this checkbox. Close the drawer. The setting of checkbox is not affecting in my fragment. This is because fragment is not refreshing.
How can I do this?Anyone please help me out. Thanks in advance.
put the code inside onResume section of activity and call onresume() wherever you want.