There is an item menu item. It is necessary that when you click on this item, the icon of this item changes and after the application is restarted, it retains the same icon. How to programmatically set it seems to understand:

item.setIcon(R.drawable.ic_english_flag_24dp); 

But after clicking on the button, recreate () happens to change the interface language, and as I understand it, the menu is redrawn from the original xml again.

Menu code:

 <?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:icon="@drawable/ic_russian_flag_24dp" android:id="@+id/action_language" android:orderInCategory="1" android:title="@string/action_language" app:showAsAction="always" /> <item android:id="@+id/action_about" android:orderInCategory="2" android:title="@string/action_about" app:showAsAction="never" /> </menu> 

How can I save the desired image for the icon?

    1 answer 1

    Save which icon to show in SharedPreferences , and onCreateOptionsMenu() set the desired icon

    To find the desired MenuItem , do this:

     MenuItem item = menu.findItem(R.id.action_language) 

    And further, as you tried:

     item.setIcon(R.drawable.ic_english_flag_24dp); 
    • Can you tell us a little more about the invoicing process? I tried to add public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem item = (MenuItem) findViewById(R.id.action_language); item.setIcon(R.drawable.ic_english_flag_24dp); return true; } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem item = (MenuItem) findViewById(R.id.action_language); item.setIcon(R.drawable.ic_english_flag_24dp); return true; } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem item = (MenuItem) findViewById(R.id.action_language); item.setIcon(R.drawable.ic_english_flag_24dp); return true; } But the application at startup gives an error. - Alexander Tymchuk
    • @Alexander Tymchuk, show the XML menu. To search for menu items, use not findViewById - Vladyslav Matviienko
    • <? xml version = "1.0" encoding = "utf-8"?> <menu xmlns: android = " schemas.android.com/apk/res/android " xmlns: app = " schemas.android.com/apk/res -auto "> <item android: icon =" @ drawable / ic_russian_flag_24dp "android: id =" @ + id / action_language "android: orderInCategory =" 1 "android: title =" @ string / action_language "app: showAsAction =" always "/> <item android: id =" @ + id / action_about "android: orderInCategory =" 2 "android: title =" @ string / action_about "app: showAsAction =" never "/> </ menu> - Alexander Tymchuk
    • @Alexander Tymchuk , add the code to the question, and format it, and I'll write the answer now - Vladyslav Matviienko
    • @Alexander Tymchuk, updated the answer. - Vladyslav Matviienko