Android Change Navigation Drawer Menu Items Text programmatically
You can change the title of Navigation Menu Item programmatically by adding following lines in MainActivity.java
file.
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... //other stuff here ... NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); // get menu from navigationView Menu menu = navigationView.getMenu(); // find MenuItem you want to change MenuItem nav_camara = menu.findItem(R.id.nav_camara); // set new title to the MenuItem nav_camara.setTitle("NewTitleForCamera"); // do the same for other MenuItems MenuItem nav_gallery = menu.findItem(R.id.nav_gallery); nav_gallery.setTitle("NewTitleForGallery"); // add NavigationItemSelectedListener to check the navigation clicks navigationView.setNavigationItemSelectedListener(this);}
This works fine for me. Hope it'll help you.
Change the code like below in case you rename or remove item from navigation menu drawer list
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); if (navigationView != null) { Menu menu = navigationView.getMenu(); menu.findItem(R.id.nav_profile).setTitle("My Account"); menu.findItem(R.id.nav_mng_task).setTitle("Control Task"); //menu.findItem(R.id.nav_pkg_manage).setVisible(false);//In case you want to remove menu item navigationView.setNavigationItemSelectedListener(this); }
If you have a layout in navigation drawer menu as an item and you want to change it programmatically, this code snippet will help you:
MenuItem nav = navigationView.getMenu().findItem(R.id.nav_close_app);nav.setActionView(R.layout.item_navigationdrawer_close_app);