Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation View set menu programmatically

In my app I want to pass certain menu depending of some condition.

This is the layout:

 <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_free"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/Color_White"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer_1"
        />

All I want to do is to change dynamically the @menu/activity_main_drawer_1 depending of some condition.

How can I do this programmatically?

like image 200
user3065901 Avatar asked Oct 15 '15 10:10

user3065901


People also ask

How to use DrawerLayout in Android?

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.


2 Answers

Use inflateMenu on your NavigationView.

For example:

navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.menu_view);

See here for more information.

like image 88
vguzzi Avatar answered Oct 18 '22 15:10

vguzzi


navigationView.getMenu().clear(); navigationView.inflateMenu(R.menu.your_menu);

like image 26
p_0g_amm3_ Avatar answered Oct 18 '22 14:10

p_0g_amm3_