Navigation drawer: How do I set the selected item at startup?
Use the code below:
navigationView.getMenu().getItem(0).setChecked(true);
Call this method after you call setNavDrawer();
The getItem(int index)
method gets the MenuItem
then you can call the setChecked(true);
on that MenuItem
, all you are left to do is to find out which element index does the default have, and replace the 0 with that index.
You can select(highlight) the item by calling
onNavigationItemSelected(navigationView.getMenu().getItem(0));
Here is a reference link: http://thegeekyland.blogspot.com/2015/11/navigation-drawer-how-set-selected-item.html
EDIT
Did not work on nexus 4, support library revision 24.0.0. I recommend use
navigationView.setCheckedItem(R.id.nav_item);
answered by @kingston below.
Android navigation drawer default selected on start
You only need to add code:
navigationView.setCheckedItem(R.id.nav_new);
NavigationView set selected item as checked
Use the code below:
navigationView.getMenu().getItem(0).setChecked(true);
Call this method after you call setNavDrawer();
The getItem(int index) method gets the MenuItem then you can call the setChecked(true); on that MenuItem, all you left to do and find out is which element index does the home the default have, and replace the 0 with that index.
How to select the first item in a navigation drawer and open a fragment on application start
In onCreate()
, following code will load the first item's fragment upon first start:
if (savedInstanceState == null) {
navigationView.getMenu().performIdentifierAction(R.id.posts, 0);
}
Thanks to calvinfly for this comment.
How to change selected Item in the navigation drawer depending on the activity/view?
Seems like you have not yet implemented the switching of the content area. I suggest you use fragments for this.
So, if you use fragments, override onAttachFragment of your activity like:
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
int id;
if(fragment instanceof HealthDiaryFragment) id = R.id.nav_healthdiary;
else
if(fragment instanceof AppointmentFragment) id = R.id.nav_appointment;
...
else return;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setCheckedItem(id);
}
Also, modify your onBackPressed:
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if(getFragmentManager().getBackStackEntryCount()>1) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
/*navigationView.getMenu().getItem(0).setChecked(true);*/
}
This is assuming that in your handling of drawer selection you replace fragments with pushing them on the back stack.
Related Topics
Android Navigation Drawer Toggle Icon to Right
View Not Attached to Window Manager Crash
CSS - Sibling Selector Not Working in Android
Single Selection in Recyclerview
Detect the Status of Two Sim Cards in a Dual-Sim Android Phone
How to Test Android Referral Tracking
How to Generate Class Diagram (Uml) on Android Studio (Intellij Idea)
How to Call a Local Web Service from an Android Mobile Application
How to Get City Name from Latitude and Longitude Coordinates in Google Maps
Error "Could Not Get Batchedbridge, Make Sure Your Bundle Is Packaged Properly" on Start of App
What Is the Fastest Way to Scrape HTML Webpage in Android
Is There a Real Solution to Debug Cordova Apps
Invoke-Customs Are Only Supported Starting with Android 0 --Min-API 26
Permission Denial: This Requires Android.Permission.Interact_Across_Users_Full