Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add items in Navigation Drawer Dynamically

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.

like image 363
Poonam Desai Avatar asked Jul 14 '26 07:07

Poonam Desai


2 Answers

try this :

final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 10; i++) {
   menu.add("Runtime item "+ i);
}
like image 58
Rujul Gandhi Avatar answered Jul 15 '26 20:07

Rujul Gandhi


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); 
like image 32
Satish Silveri Avatar answered Jul 15 '26 19:07

Satish Silveri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!