Mehran
Mehran

Reputation: 501

Save optionMenu item icon to shared preferences

I have a list of data with two different views in my Fragment, my default view is Listview and another is Gridview. I switch between this two views by clicking on an icon on my toolbar. And I set the switch item icon dynamically inside onOptionsItemSelected method like this:

//global variables
private int currentViewMode = 0 ;
static final int VIEW_MODE_LISTVIEW = 0;
static final int VIEW_MODE_GRIDVIEW = 1;
.
.
.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.switchView:
            if (VIEW_MODE_LISTVIEW == currentViewMode){
                item.setIcon(R.mipmap.ic_gridview);
                currentViewMode = VIEW_MODE_GRIDVIEW;
            }else {
                item.setIcon(R.mipmap.ic_listview);
                currentViewMode = VIEW_MODE_LISTVIEW;
            }
            switchView();
            SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putInt("currentViewMode",currentViewMode);
            editor.commit();
            return false;

        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}

And I get my view from shared preferences like this:

SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",activity.MODE_PRIVATE);
currentViewMode = sharedPreferences.getInt("currentViewMode",VIEW_MODE_LISTVIEW);

But I don't know how can I save the item icon and how to retrive it.

Can you help me please?

Menu

<?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/switchView"
    android:title=""
    android:icon="@mipmap/ic_listview"
    app:showAsAction="always"/>

</menu>

Upvotes: 1

Views: 1080

Answers (1)

S.R
S.R

Reputation: 2829

You can add another item inside your menu.xml for your gridView Then create a boolean that carries the visibility of this two items. when you switch to listview, set the visibility of gridItem to false, And when you switch to gridview, set the listItem to false; In this way you'll be able to save and retrieve the visibilities with shared preferences as a boolean.

Change your code like this:

<item
    android:id="@+id/switchListView"
    android:title=""
    android:icon="@mipmap/ic_listview"
    app:showAsAction="always"/>

<item
    android:id="@+id/switchGridView"
    android:title=""
    android:icon="@mipmap/ic_gridview"
    android:visible="false"
    app:showAsAction="always"/>

Inside your Fragment:

boolean isGridView;

Then:

SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
isGridView = myPrefs.getBoolean("menu_item", false);

And finally change your onOptionItemSelected method like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
    final SharedPreferences.Editor editor = myPrefs.edit();
    isGridView = myPrefs.getBoolean("menu_item", false);

    switch (item.getItemId()) {

        case R.id.switchListView:

            if (VIEW_MODE_LISTVIEW == currentViewMode){
                isGridView=true;
                editor.putBoolean("menu_item", isGridView);
                editor.commit();
                activity.invalidateOptionsMenu();
                item.setIcon(R.mipmap.ic_gridview);
                currentViewMode = VIEW_MODE_GRIDVIEW;
            }else {
                isGridView=false;
                editor.putBoolean("menu_item", isGridView);
                editor.commit();
                activity.invalidateOptionsMenu();
                item.setIcon(R.mipmap.ic_listview);
                currentViewMode = VIEW_MODE_LISTVIEW;
            }
            switchView();
            SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
            SharedPreferences.Editor et = sharedPreferences.edit();
            et.putInt("currentViewMode",currentViewMode);
            et.commit();
            return false;





        case R.id.switchGridView:
            if (VIEW_MODE_LISTVIEW == currentViewMode){
                isGridView=true;
                editor.putBoolean("menu_item", isGridView);
                editor.commit();
                activity.invalidateOptionsMenu();
                item.setIcon(R.mipmap.ic_gridview);
                currentViewMode = VIEW_MODE_GRIDVIEW;
            }else {
                isGridView=false;
                editor.putBoolean("menu_item", isGridView);
                editor.commit();
                activity.invalidateOptionsMenu();
                item.setIcon(R.mipmap.ic_listview);
                currentViewMode = VIEW_MODE_LISTVIEW;
            }
            switchView();
            sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
            et = sharedPreferences.edit();
            et.putInt("currentViewMode",currentViewMode);
            et.commit();
            return false;

        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    if(isGridView==true){
        menu.findItem(R.id.switchListView).setVisible(false);
        menu.findItem(R.id.switchGridView).setVisible(true);
    }else{
        menu.findItem(R.id.switchListView).setVisible(true);
        menu.findItem(R.id.switchGridView).setVisible(false);
    }

    super.onPrepareOptionsMenu(menu);
}

Important note

How to implement optionMenu inside Fragments:

1. If you have implemented optionMenu inside your MainActivity, then you should return false in your MainActivity's onOptionsItemSelected, otherwise your Fragment's optionMenu will not work.

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
        //
    }
    return false;
}

2. you have to setHasOptionsMenu(true); inside your onCreate in your Fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

Upvotes: 4

Related Questions