[JAVA] [Android] Gray out Menu Item in Options Menu

Overview

I tried to gray out some MenuItems in OptionsMenu under certain conditions so that they could not be tapped, so I wrote it as a memorandum.

Implementation of Options Menu

MainFragment.java


private boolean isEnable = true;

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    inflater.inflate(R.menu.menu_item, menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    int id = item.getItemId();
    switch (id) {
        case R.id.menu_first:
            activity.getSupportActionBar().invalidateOptionsMenu();
            isEnable = false
            return true;
        case R.id.menu_second:
            return true;
    }
    return super.onOptionsItemSelected(item);
}

This time it was implemented in Fragment, so it is described in Fragment Note that the Options Menu will not be displayed unless you do setHasOptionsMenu (true) in ʻActivity`.

menu_item.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_first"
        android:orderInCategory="1"
        android:title="Menu 1"
        app:showAsAction="never" />
    <item
        android:id="@+id/menu_second"
        android:orderInCategory="2"
        android:title="Menu 2"
        app:showAsAction="never" />
</menu>

Put ʻapp: showAsAction =" never "` to store in the overflow menu

Update OptionsMenu menu with onPrepareOptionsMenu

MainFragment.java


@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
    menu.findItem(R.id.menu_second).setEnabled(isEnable);
}

If you are not using Toolbar in your layout file, you can gray out by just pressing setEnable, but if you are using Toolbar, justsetEnable will not gray out. Use Theme to specify the attributes of ʻapp: popupTheme`

If you are using Toolbar

menu_item_text.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#727272" />
    <item android:color="#000000"/>
</selector>

Create a selector in the color package, make the menu text gray if it is false, and black otherwise.

styles.xml


<style name="AppThemeOverlay.AppBar.Menu" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColor">@color/menu_item_text</item>
</style>

Create a Theme with styles so that the menu text is grayed out by using the selector created earlier

fragment_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:popupTheme="@style/AppThemeOverlay.AppBar.Menu" />

</LinearLayout>

If you do ʻapp: popupTheme =" @ style / AppThemeOverlay.AppBar.Menu "inToolbar, even if you implement Toolbarindependently, it will be grayed out just byset Enable`

reference

Gray out the menu on Android-Tadao Yamaoka's diary

Recommended Posts

[Android] Gray out Menu Item in Options Menu
Notes in Android studio