Pavvi
Pavvi

Reputation: 73

Change values in menu xml

In my android app I got main_activity.xml:

    <android.support.design.widget.NavigationView
    android:id="@+id/nav"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/head"
    app:menu="@menu/nav_menu"/>

I would like to change app:menu value programatically, is there any way to do it?

Upvotes: 1

Views: 63

Answers (2)

Priyank Patel
Priyank Patel

Reputation: 12382

Crate a NavigationView object by findViewById

    NavigationView mNavigationView = (NavigationView) findViewById(R.id.left_drawer);

You can change it at runtime by using below code...

    mNavigationView.getMenu().clear();
    mNavigationView.inflateMenu(R.menu.menu);

Upvotes: 1

user4326368
user4326368

Reputation:

Use the findViewById() method to get a reference to the NavigationView object.

The NavigationView class provides a method inflateMenu(resourceID).

Link: https://developer.android.com/reference/android/support/design/widget/NavigationView.html#inflateMenu%28int%29

Upvotes: 2

Related Questions