Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - Enable and Disable MenuItem on Navigation Drawer Activity Layout

I have and app created using the Navigation Drawer Activity Layout.. And I need to enable/disable some items programmatically

Menu Example

Example: I want to disable the Menu Item id nav_item2

I tried to do...

MenuItem nav_item2 = (MenuItem)findViewById(R.id.nav_item2);
nav_item2.setEnabled(false);

And it return a NullPointerException to me..

Also tried...

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    nav_item2 = menu.findItem(R.id.nav_item2);
    return true;
}

with the same NullPointerException

Someone know something to do?

nav_header_main_activity.xml (Generated with the Layout Creation)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="199dp"
        android:layout_height="72dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@drawable/ciag" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="CIAg"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[email protected]" />

</LinearLayout>

main_activity_navigation_drawer.xml (generated on the layout creation)

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_save"
        android:icon="@android:drawable/ic_menu_save"
        android:enabled="false"
        android:title="@string/nav_save" />
    <item
        android:id="@+id/nav_graph"
        android:icon="@drawable/chart_bar"
        android:enabled="false"
        android:title="@string/nav_graph" />
    <item
        android:id="@+id/nav_send"
        android:icon="@drawable/ic_menu_send"
        android:title="Enviar" />
</group>

<item android:title="Configurações">
    <menu>
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/ic_menu_manage"
            android:enabled="false"
            android:title="@string/nav_manage" />
        <item
            android:id="@+id/nav_support"
            android:icon="@android:drawable/ic_menu_help"
            android:title="@string/nav_support" />
    </menu>
</item>

like image 661
Helcio R. Macedo Pandelo Avatar asked Apr 29 '16 13:04

Helcio R. Macedo Pandelo


People also ask

How do I customize my navigation drawer?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.

Can we use activity in navigation drawer?

As for how to switch between activities via the navigation drawer, you can just set up new intents within your selectItem() method: private void selectItem(int position) { // Handle Navigation Options Intent intent; switch (position) { case 0: intent = new Intent(currentActivity, NewActivity. class); intent.


3 Answers

You can access menu from NavigationView.getMenu()

Below is code :

NavigationView navigationView= findViewById(R.id.nav_id_in_layout)

Menu menuNav=navigationView.getMenu();
MenuItem nav_item2 = menuNav.findItem(R.id.nav_item2);
nav_item2.setEnabled(false)
like image 102
N J Avatar answered Nov 16 '22 01:11

N J


Use below code

navigationView.getMenu().findItem(R.id.your_item_id).setEnabled(false);

More details is here

like image 38
Faxriddin Abdullayev Avatar answered Nov 16 '22 03:11

Faxriddin Abdullayev


If you're using view binding you could do next thing:

binding.navigationDrawer.menu.children.forEach { menuItem ->
    menuItem.isEnabled = true // or false, what you need
}
like image 24
Alejandro Ochoa Avatar answered Nov 16 '22 03:11

Alejandro Ochoa