Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inflate android NavigationView with another menu dynamically during onClick?

Hi I have a NavigationView and there is an imageview in the headerview of that NavigationView. When I click on the imageview , NavigationView should inflate another menu resource file and replace current menu like in PlayStore app.

I've tried this:

imgIndic.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.e("Onclick","Arrow click");
        navigationView.inflateMenu(R.menu.second_menu);
    }
}); 

But when I click on the button, nothing happens! (Log Onclick is showing in the logcat) How can I do this?

like image 676
Sudheesh Mohan Avatar asked Jul 03 '15 09:07

Sudheesh Mohan


2 Answers

I would suggest to inflate just one menu with several groups and change the visibility of the groups. Simple and effective.

<group
    android:id="@+id/group_1"
    android:checkableBehavior="single"
    android:visible="false">
    ...
</group>

<group
    android:id="@+id/group_2"
    android:checkableBehavior="single"
    android:visible="true">
    ...
</group>

In your java code call

navigationView.getMenu().setGroupVisible(R.id.group_1, true)
like image 78
Lamorak Avatar answered Nov 10 '22 03:11

Lamorak


try the following code

imgIndic.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {      
        navView.getMenu().clear();
        navView.inflateMenu(R.menu.second_menu);
    }
}); 
like image 40
Moinkhan Avatar answered Nov 10 '22 01:11

Moinkhan