I create Drawer.But I want to set Itemlist of drawer is dynamically.Means get data from database and set as drawerList. Is It Possible? and yes than How?I know static drawer as well.
-
Are you want to Load navigation drawer itmes dynamically right? – Ravish Sharma Dec 03 '16 at 10:26
-
Yes. all are from database. – Poonam Desai Dec 03 '16 at 10:29
-
Possible duplicate: http://stackoverflow.com/questions/31722566/dynamic-adding-item-to-navigationview-in-android – Mohammad Zarei Dec 03 '16 at 10:33
-
http://stackoverflow.com/a/31722855/2372591 – Shivang Trivedi Dec 03 '16 at 10:41
4 Answers
try this :
final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 10; i++) {
menu.add("Runtime item "+ i);
}

- 1,700
- 13
- 28
-
Thanks... But i dont want to like this. I want set database data to it. – Poonam Desai Dec 03 '16 at 10:37
-
2yes you can use this. you have to put this code onpostExecute() if you are using asyncTask. – Rujul Gandhi Dec 03 '16 at 10:38
-
-
Thank you Sir .It is working fine .How to add id for this dynamic menu . – Ganesan J May 20 '21 at 06:39
-
@GanesanJ You have to use this menu.add(0// group Id, R.id.yoru_id,100 // order, "data"//title); – Rujul Gandhi May 20 '21 at 08:05
YES it is possible this will be your main layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/login_drawer"
>
<LinearLayout
android:id="@+id/linearlayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
//create you toolbar and include in here
<include
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></include>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
<include layout="@layout/drawerlayout" />
</android.support.v4.widget.DrawerLayout>
and you drawer layout will be like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_linear"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
now you can create another layout which will be the item of drawer menu ie if its only text then make a layout with only textview else if it is image and text then make the layout accordingly
AND then just add this view(child xml) dynamically by using layoutinflater and adding view in linearlayout like:
linearlayout.addView(childview);

- 393
- 4
- 9
thanks to @Dev
To add the Item dynamically, we can get a Menu object using getMenu() method of NavigationView and then we can add Items into the navigation drawer using that Menu object.
like this :
final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 3; i++) {
menu.add("Runtime item "+ i);
}
Using SubMenu, we can add a subsection and Items into it.
// adding a section and items into it
final SubMenu subMenu = menu.addSubMenu("SubMenu Title");
for (int i = 1; i <= 2; i++) {
subMenu.add("SubMenu Item " + i);
}
//

- 544
- 6
- 9
-
1But how to attach Fragment to these which should open when they are pressed by user ? – SimpleGuy Sep 27 '20 at 14:32
-
@SimpleGuy you can override the function `onNavigationItemSelected(MenuItem)` or use the function `Toolbar#setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener)` and instead of checking the id of that menu, you can check the title which was assigned when you added that menu item. – Iyxan23 Aug 04 '21 at 14:09
Narrow down your search to Navigation Drawer with List view. If you have listview you can manipulate data with adapter.
Check this one example. https://youtu.be/rs4LW3GxOgE

- 97
- 1
- 5