1

I'm developing an app using the official android NavigationDrawer, the navDrawer has its own menu xml called activity_main_drawer.xml in the picture below:

activity_main_drawer screenshot

This is it in the app:

app screenshot

Now i want to change the titles and icons for each row dynamically (JSON), I know how to make a custom ListView Adapter but i couldn't know how to make a custom menu adapter for this navDrawer. It's not a ListView, it's a menu and has groups and items.

Any help is very appreciated.

Fadi Obaji
  • 1,454
  • 4
  • 27
  • 57
  • Possible duplicate of [How to create a custom navigation drawer in android](http://stackoverflow.com/questions/21796209/how-to-create-a-custom-navigation-drawer-in-android) – random Aug 17 '16 at 06:25

2 Answers2

1

You need to add menu items dynamically like below:

Menu menu = nvDrawer.getMenu();
        for (MenuItem mi : menu.values()) {
            if (menu.size() == 0) {
               menu.add(mi.getId() + "");
            }
            if (menu.getItem(mi.getId()) == null) {
                menu.add(mi.getId() + "");
            }
            MenuItem mi = menu.getItem(mi.getId());
            mi.setIcon(mi.getIcon());
            mi.setTitle(mi.getTittle());
        }
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

U can add ListView or Recycler View as navigation drawer just u required a xml tag in your view is

android:layout_gravity="start" // This is indicates your navigation drawer

after that your layout should be like below (You can replace Recyclerview with Listview) RelativeLayout rl_drawer_nav_bar will work as a navigation drawer. After this you doesn't required inbuilt provided NavigationDrawer View

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<LinearLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />

</LinearLayout>

<RelativeLayout
    android:id="@+id/rl_drawer_nav_bar"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start">

    <RelativeLayout
        android:id="@+id/rl_drawer_main_header_title_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <ImageView
            android:id="@+id/iv_drawer_user_image"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/menu_logo" />

        <TextView
            android:id="@+id/tv_drawer_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />


    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_drawer_menu_list"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_below="@+id/rl_drawer_main_header_title_bar"
        android:layout_gravity="start"
        android:choiceMode="singleChoice" />

</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

After your API response pars JSON to List YourMenuModel and

YourCustomAdapter adapter = new YourCustomAdapter(yourMenuModelList)

Whenever your menu list changes or updates or you adds new menu, just call adapter.notifyDataSetChanged().

Mitul Varmora
  • 3,832
  • 1
  • 12
  • 21
  • I know i can do that, but i specifically mentioned that i want to keep the menu items and alter it dynamically, it looks better that the listview – Fadi Obaji Aug 17 '16 at 13:08