简体   繁体   中英

Howto change the Icon MenuItem android app

I would like to implement a day-night mode switch in a MenuItem. If the day mode is selected, a moon should be displayed, and if the night mode is selected a sun should be diplayed. I save the settings in SharedPreferences and want to load them again when starting the app.

In my MainActivity-Class i definded

  public static final String NIGHT_MODE = "night_mode";
  private boolean safedNightMode;

This is my onCreate-method for loading the SharedPreferences

 protected void onCreate(Bundle savedInstanceState) {

        requireNonNull(getSupportActionBar()).setDisplayShowHomeEnabled(true);
        Objects.requireNonNull(getSupportActionBar()).setLogo(R.mipmap.logo_psc_round);
        getSupportActionBar().setDisplayUseLogoEnabled(false);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
        safedNightMode = sharedPreferences.getBoolean(NIGHT_MODE,false);
    
        if (safedNightMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }

My onCreateOptionsMenu an my onOtionsItemSelected-method

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_clean:
                openDialog();
                return true;
            case R.id.item_dn_switch:
                switchDayNightMode();
                return true;
            case R.id.item_recipe:
                Intent intentRecipe = new Intent(this, RecipeActivity.class);
                startActivity(intentRecipe);
                return true;
            case R.id.item_about:
                Intent intentAbout = new Intent(this, AboutActivity.class);
                startActivity(intentAbout);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

My switchDayNightMode-method will called when the item is selected

public void switchDayNightMode() {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
        safedNightMode = sharedPreferences.getBoolean(NIGHT_MODE,false);
        nightMode = safedNightMode;
        if (safedNightMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            nightMode = false;
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            nightMode = true;
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(NIGHT_MODE, nightMode);
        editor.apply();
    }

My menu.xml is here. The relevant item is android:id="@+id/item_dn_switch".

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/item_clean"
        android:title="@string/txt_menu_item_counter_reset"
        android:icon="@drawable/ic_baseline_delete_forever_24"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/item_dn_switch"
        android:title="@string/txt_menu_item_dnswitch"
        android:icon="@drawable/ic_baseline_mode_night_24"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/item_recipe"
        android:title="@string/txt_menu_item_recipe"
        android:icon="@drawable/ic_baseline_list_24"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/item_about"
        android:title="@string/about"
        android:icon="@drawable/ic_baseline_contact_support_24"
        app:showAsAction="ifRoom"/>
</menu>

How can I realize that the icon is changed depending on the day-night-mode?

You can change the icon to night by creating a folder with icons for the night theme. Create this folder (click RightClick on res folder) and set values like below on screen. After creating you should move night icons to this new folder. The icons should automatically switch to the night theme.

屏幕

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM