Adding an item programatically in navigation drawer menu - android

I have a navigation drawer in my application. I want to add an option programatically in the second section of the navigation drawer's menu. The menu.xml is-
<group android:checkableBehavior="single">
<item android:id="#+id/nav_connect_sensor"
android:icon="#drawable/ic_action_bluetooth"
android:title="Connect Bluetooth" />
<item android:id="#+id/nav_take_tour"
android:icon="#drawable/ic_action_video"
android:title="Take a tour" />
<item android:id="#+id/nav_my_stats"
android:icon="#drawable/ic_action_bargraph"
android:title="My Stats" />
</group>
<item android:title="#string/navigation_drawer_group_title"
android:id="#+id/other_labels">
<menu>
<item android:id="#+id/nav_myAccountSettings"
android:icon="#drawable/ic_settings"
android:title="My Account" />
<item android:id="#+id/nav_application_settings"
android:icon="#drawable/ic_action_settings"
android:title="Application Settings" />
<item android:id="#+id/comparison"
android:icon="#drawable/ic_swap_horiz"
android:title="Compare"/>
<item android:id="#+id/nav_help_and_feedback"
android:icon="#drawable/ic_live_help"
android:title="Help and Feedback" />
<item android:id="#+id/nav_logout"
android:icon="#drawable/ic_action_exit"
android:title="Sign Out" />
</menu>
</item>
For adding an option programatically-
Menu menu = mNavigationView.getMenu();
menu.add(0, Menu.FIRST, Menu.FIRST, "Players")
.setIcon(R.drawable.ic_people);
This option is getting added at the last. How can I add the option in the third position i.e. below Application Settings

Try this:
Menu.THIRD=3;
Menu menu = mNavigationView.getMenu();
menu.add(0, Menu.THIRD, 2, "Players")
.setIcon(R.drawable.music_audio);

You can use.
Menu menu = navView.getMenu(); // navView is NavigationView reference.
menu.add("Item 1");
menu.add("Item 2");
menu.add("Item 3");

Related

Add item to navigationView

I have made this .xml that contains items of my navigationView.
<item android:title="Title">
<menu>
<group
android:id="#+id/menu"
android:checkableBehavior="single">
<item
android:id="#+id/a"
android:title="A"
android:orderInCategory="1"
/>
<item
android:id="#+id/b"
android:title="B"
android:orderInCategory="1"
/>
<item
android:id="#+id/c"
android:title="C"
android:orderInCategory="1"
/>
</group>
</menu>
</item>
I want to add items bellow item C on the same group programmatically?
In your code:
Menu menu = navigationView.getMenu();
//params: groupId, itemId, order, title
menu.add(R.id.menu, 123, Menu.NONE, "Item");
to remove the divider add this line to your style xml:
<item name="android:listDivider">#android:color/transparent</item>
Use method
navigationView.getMenu().add(int groupId, int itemId, int order, CharSequence title);
Like
getMenu.add(R.id.submenu_others, R.id.action_logout, Menu.NONE, "logout");

Remove Divider from NavigationView items

I made a navigation view populated by this .xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Menu A"
>
<menu
>
<group
android:checkableBehavior="single"
android:orderInCategory="2"
android:id="#+id/gr"
>
<item
android:id="#+id/a1"
android:title="A1"
/>
</group>
</menu>
</item>
<item android:title="Menu B"
android:orderInCategory="3"
>
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/b1"
android:title="B1"
/>
<item
android:id="#+id/b2"
android:title="B2"
/>
<item
android:id="#+id/b3"
android:title="B3"
/>
</group>
</menu>
</item>
I want to add one item programatically to my NavigationView.
I am adding that item with this line of code:
MenuItem item = menu.add(R.id.gr, Menu.NONE, 2, "A2");
Above that item (A2) appears a delimiter. I don't want that delimiter. How can I remove it?
Note: I don't want to put the color of my delimiters transparent. That will make all delimiters disappear.
Add the new item to the SubMenu, not to the Menu. So the delimiter above the item will not appear.
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.getItem(0);
SubMenu subMenu = menuItem.getSubMenu();
subMenu.add(R.id.gr, Menu.NONE, 2, "A2");
subMenu.setGroupCheckable(R.id.gr, true, true);
Call subMenu.setGroupCheckable(R.id.gr, true, true) if you want to define the checkable behaviour for the group. This should be called after the items of the group have been added.

Add items to menu group programatically in navigation view

I am trying to add menu items to menu group pragmatically but I found no way to do that. I am using Navigation View and added below mentioned menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_lang_currency"
android:title="" />
<item
android:id="#+id/nav_home"
android:title="" />
<group android:id="#+id/nav_refer" />
<item
android:id="#+id/nav_setting"
android:title="" />
<item
android:id="#+id/nav_about_us"
android:title="" />
<item
android:id="#+id/nav_logout"
android:title="" />
</menu>
Everything looks good as mentioned.
I just want to add multiple menu items in nav_refer group at run-time as per business requirement but I found no way to do that.
I searched solution on SO but found no way to do that.
Kindly suggest me how to add multiple menu items in group at run-time.
To add menu to a particular group, call this method Menu.add(int groupId, int itemId, int order, CharSequence title)
Menu menu = navigationView.getMenu();
menu.add(R.id.nav_refer, 123, Menu.NONE, "Title1");
menu.add(R.id.nav_refer, 124, Menu.NONE, "Title2");
menu.add(R.id.nav_refer, 125, Menu.NONE, "Title3");
Important : Initially if you have empty group then newly added items will appear in bottom, to solve this you need to mention orders for groups. add a attribute for all your groups android:orderInCategory="101"
You can do something like this:
NavigationView navView = (NavigationView) findViewById(R.id.navView);
Menu menu = navView.getMenu();
SubMenu subMenu = menu.addSubMenu("sub menu");
subMenu.add("item 1");
subMenu.add("item 2");
subMenu.add("item 3");
create res --> menu file new layout
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item android:title="حساب کاربری">
<menu>
<group android:title="حساب کاربری">
<item android:title="ورود" />
<item android:title="ثبت نام" />
</group>
</menu>
</item>
</group>
<group android:checkableBehavior="single">
<item android:title="سایت">
<menu>
<group android:title="حساب کاربری">
<item android:title="ورود" />
<item android:title="ثبت نام" />
</group>
</menu>
</item>
</group>
</menu>
set NavigationView to app:menu="#menu/drawer_view"
i hope help you

How to show/hide submenus inside NavigationView android

I want to create an expandablelistview like view inside navigation drawer where initially only main titles are shown (collapsed submenus). When title is clicked the submenus should appear (if possible animate)
I am using android.support.design.widget.NavigationView. I am able to add submenus via xml.
<group android:checkableBehavior="all">
<item
android:id="#+id/action_venues"
android:title="TITLE 1">
<menu>
<group
android:id="#+id/group1"
android:checkableBehavior="single"
>
<item android:title="SUBTITLE1.1" />
<item android:title="SUBTITLE1.2" />
<item android:title="SUBTITLE1.3" />
</group>
</menu>
</item>
<item
android:id="#+id/action_vendors"
android:title="TITLE2">
<menu>
<group
android:id="#+id/group2"
android:checkableBehavior="single"
android:visible="false">
<item android:title="SUBTITLE2.1" />
<item android:title="SUBTITLE2.2" />
<item android:title="SUBTITLE2.3" />
</group>
</menu>
</item>
But if I keep any group in the xml android:visible="false" it doesn't appear in the drawer.
I also tried programatically showing/hiding the submenu with:
subMenu1.setGroupVisible(R.id.group1,false);
But it just hides the title menu also.
Is it possible with android.support.design.widget.NavigationView or do I have to use a custom view?
Menu menu = navigationView.getMenu();
for (int i = 0; i < menu.size(); i++) {
if (menu.getItem(i).getItemId() != R.id.login){
menu.getItem(i).setVisible(false);
}
}
I managed to show/hide the subtitles by making the titles a separate group. Not sure if this is the right way:
<group>
<item
android:id="#+id/action_venues"
android:title="group1" />
</group>
<group
android:id="#+id/group2"
android:checkableBehavior="single">
<item android:title="SUBTITLE1.1" />
<item android:title="SUBTITLE1.2" />
<item android:title="SUBTITLE1.3" />
</group>
<group>
<item
android:id="#+id/action_vendors"
android:title="group3" />
</group>
<group
android:id="#+id/group4"
android:checkableBehavior="single"
android:visible="false">
<item android:title="SUBTITLE2.1" />
<item android:title="SUBTITLE2.2" />
<item android:title="SUBTITLE2.3" />
</group>
And then in the code:
Menu drawerMenu = drawer.getMenu();
case R.id.action_venues:
drawerMenu.setGroupVisible(R.id.group2,true);
case R.id.action_vendors:
drawerMenu.setGroupVisible(R.id.group4,true);
But still can't animate.
For the group tags I used
navigationView.getMenu().setGroupVisible(false)
For the item tags I used
navigationView.getMenu().findViewById(R.id.id_of_the_item).setVisibility(View.GONE)
All working.
navigationView.getMenu().findItem(R.id.id_of_the_item).setVisible(false)

Change NavigationView items when user is logged

My app's main activity has a navigation drawer, instantiated in the XML in this way:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/application_drawer"
android:background="#color/white"/>
Now, the menu entry for the navigation drawer is the following:
<?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/login"
android:icon="#drawable/ic_action_person"
android:title="#string/login"/>
<item
android:id="#+id/settings"
android:icon="#drawable/ic_action_settings"
android:title="#string/settings"/>
<item
android:id="#+id/terms"
android:icon="#drawable/ic_action_about"
android:title="#string/terms_and_conditions_menu"/>
<item
android:id="#+id/about"
android:icon="#drawable/ic_action_about"
android:title="#string/info_hotelsclick"/>
</group>
What I'd like to do is to change the first item (and possibly the others as well) dynamically under some conditions. For instance, I'd like to change the "Login" entry with a "logout" one once the user has logged in ;-)
How can I achieve that? I managed to add an item to the Drawer in this way
Menu menu = navigationView.getMenu();
menu.add("Test");
but it doesn't sound that good to me, I'm pretty sure there must be a cleaner way.
...but does it?
I thing the best approach to this is to include all your items in the menu and the change their visibility.
<item
android:id="#+id/login"
android:icon="#drawable/ic_action_person"
android:title="#string/login"
android:visible="true" />
<item
android:id="#+id/logout"
android:icon="#drawable/ic_action_person"
android:title="#string/logout"
android:visible="false" />
then
navigationView.getMenu().findItem(R.id.login).setVisible(false);
navigationView.getMenu().findItem(R.id.logout).setVisible(true);
You can also do this with whole groups of items
<group
android:id="#+id/group_1"
android:checkableBehavior="single"
android:visible="false">
...
</group>
and
navigationView.getMenu().setGroupVisible(R.id.group_1, true)
Simple solution:
Add two xml files in menu directory:
navigation_with_login.xml Navigation Menu for logged in users
<?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_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_login"
android:icon="#drawable/ic_menu_login"
android:title="Login" />
</group>
</menu>
navigation_with_logout.xml Navigation Menu for default user:
<?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_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_menu_logout"
android:title="Logout" />
</group>
</menu>
Now you can change NavigationView items,just write few lines of code.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin)
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.navigation_with_login);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.navigation_with_logout);
}
Frst get the navigationmenu
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
To add the menu dynamically
if(loggedOut){
menu.add(R.id.submenu_others, R.id.action_logout, Menu.NONE, "logout");
}
Here is menu.add(groupId, menuItemId, orderOfMenu, menuItem text)
if(loggedIn){
menu.removeItem(R.id.action_logout);
}
I got it done, try this when you need to change the title:
navigationView.getMenu().findItem(R.id.yourItemId).setTitle("my title");
Hope it helped!

Categories

Resources