Android Change Navigation Drawer Menu Items Text programmatically
Asked Answered
H

6

48

I have the new Navigation Drawer in my app and I want to change the navigation view menu items title text dynamically from code. I have watched many posts but I can't figure out, how can I do this. How can I achieve this correctly?

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

}

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">
    <item android:id="@+id/nav_camara" android:icon="@android:drawable/ic_menu_camera"
        android:title="Import" />
    <item android:id="@+id/nav_gallery" android:icon="@android:drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item android:id="@+id/nav_slideshow" android:icon="@android:drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item android:id="@+id/nav_manage" android:icon="@android:drawable/ic_menu_manage"
        android:title="Tools" />
</group>

<item android:title="Communicate">
    <menu>
        <item android:id="@+id/nav_share" android:icon="@android:drawable/ic_menu_share"
            android:title="Share" />
        <item android:id="@+id/nav_send" android:icon="@android:drawable/ic_menu_send"
            android:title="Send" />
    </menu>
</item>

Hairy answered 22/10, 2015 at 15:29 Comment(1)
The only way you can change the menu dynamically is to clear it and load another menu: #31203712Irresistible
N
125

You can change the title of Navigation Menu Item programmatically by adding following lines in MainActivity.java file.

@Override
protected 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.

Nudge answered 15/12, 2015 at 8:2 Comment(0)
S
12

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);
    }
Sauncho answered 22/9, 2016 at 12:59 Comment(0)
C
4

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);
Conformal answered 2/7, 2019 at 11:45 Comment(0)
B
3
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menus);


    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Menu menu = navigationView.getMenu();
    MenuItem nav_login = menu.findItem(R.id.nav_logout);
nav_login.setTitle("Login");


}

This is correct! Hope this is running fine......

Bluefield answered 9/1, 2018 at 10:31 Comment(0)
A
2

This code can be used to add menus dynamically. it worked for me... the main part of the code is the displayItems();.....pass the String[] of items as a parameter to the function....it works.

public class Courses extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    TextView nn,ne,np,nl;
    SharedPreferences sp;
    NavigationView navigationView;
    DrawerLayout drawer;
    ImageView img;
    NavigationView nv;
    int id;

String[] acc={"ACPFAT","CPFA","Tally.ERP 9 Simplified","D P F A","CA Articles","DAA","TFAP","Tally.ERP 9 - Specialization","GST (Goods and service Tax)"};

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setDrawerTitle(Gravity.CENTER,n);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, 
                 R.string.navigation_drawer_open,R.string.navigation_drawer_close);

drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);

Intent i=getIntent();
id=i.getIntExtra("button",0);

switch(id)
    {

        case R.id.account:
            img.setImageResource(R.drawable.acc);
            displayItems(acc);
            break;

        case R.id.modular:
            img.setImageResource(R.drawable.acc);
            displayItems(mod);
            break;

        case R.id.diploma:

            img.setImageResource(R.drawable.acc);
            displayItems(dc);
            break;

    }

public void displayItems(String[] a)
  {

    final NavigationView navigationView=findViewById(R.id.nav_view);
    final DrawerLayout drawer=findViewById(R.id.drawer_layout);
    Menu menu=navigationView.getMenu();
    SubMenu sb=menu.addSubMenu("Courses");

    int i=0;
    while(i<a.length)
    {
        sb.add(a[i]);
        i++;
    }
 }
Anecdotic answered 21/6, 2018 at 4:3 Comment(0)
A
1

For Kotliin :

val bottomNavigationView = findViewById<BottomNavigationView(R.id.bottom_nav)
        
val menu: Menu = bottomNavigationView.menu
val secondMenu: MenuItem = menu.findItem(R.id.needsFragment)
        // To set menu title according to user type (ie. Abc / Xyz )
        when(userType){
            1 -> secondMenu.title = "Abc"
            2 -> secondMenu.title = "Xyz"
        }
        bottomNavigationView.setupWithNavController(navController)
        bottomNavigationView.setOnNavigationItemReselectedListener { }
Ashraf answered 27/7, 2021 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.