2

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.

Poonam Desai
  • 85
  • 1
  • 1
  • 8

4 Answers4

8

try this :

final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 10; i++) {
   menu.add("Runtime item "+ i);
}
Rujul Gandhi
  • 1,700
  • 13
  • 28
1

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); 
Satish Silveri
  • 393
  • 4
  • 9
1

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);
}

//

mahmoud zaher
  • 544
  • 6
  • 9
  • 1
    But 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
0

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

Vurgun M
  • 97
  • 1
  • 5