Add, hide, show, highlight items in NavigationView in Android

You can programmatically add, hide or show items in NavigationView in Android.

First set your Android Project with NavigationView from Android Studio.

create new android project with navigation view in android

create new android project with navigation view in android

Follow the below code for that.

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

//add new item at the end of NavigationView menu
MenuItem mi = navigationView.getMenu().add(Menu.NONE, Menu.NONE, Menu.NONE, "Menu Item 1");

//set icon for that menu item
mi.setIcon(R.drawable.ic_icon_menu_item);

//hide the menuitem
mi.setVisible(false);

//show the menuitem
mi.setVisible(true);

//highlight the menuitem
mi.setChecked(true);

//un-highlight the menuitem
mi.setChecked(false);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s