1

I have multiple buttons i want to change color of button . that i know , but how to save those colors in shared preference ?? and how to delete them from shared preferences ??

private void ShowPunch() {
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.fragment_edit_profile);

                WS = (Button)dialog.findViewById(R.id.ws);
                WE = (Button)dialog.findViewById(R.id.we);
                LS = (Button)dialog.findViewById(R.id.lts);
                LE = (Button)dialog.findViewById(R.id.lte);
                PS = (Button)dialog.findViewById(R.id.pts);
                PE = (Button)dialog.findViewById(R.id.pte);
                MRMS = (Button)dialog.findViewById(R.id.mrms);
                MRME = (Button)dialog.findViewById(R.id.mrme);

                WS.setOnClickListener(this) ;
                WE.setOnClickListener(this) ;
                LS.setOnClickListener(this) ;
                LE.setOnClickListener(this) ;
                PS.setOnClickListener(this) ;
                PE.setOnClickListener(this) ;
                MRMS.setOnClickListener(this) ;
                MRME.setOnClickListener(this) ;
                dialog.show();
            }

            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {


                switch (v.getId()) {
                    case R.id.ws:
                        Punching();
                        WS.setBackgroundColor(R.color.feedpos);
                        Toast.makeText(context, "Your Work Time Start From Now..", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.we:
                        Punchingwe();
                        Toast.makeText(context, "Your Work Time End Here..", Toast.LENGTH_SHORT).show();
                        WE.setBackgroundColor(myIntValue);

                        break;
                    case R.id.pts:
                        Punchingpts();
                        Toast.makeText(context, "Your Prayer Time Start From Now..", Toast.LENGTH_SHORT).show();
                        PS.setBackgroundColor(myIntValue);
                        break;
                    case R.id.pte:
                        Punchingpte();
                        Toast.makeText(context, "Your Prayer Time End Here..", Toast.LENGTH_SHORT).show();
                        PE.setBackgroundColor(myIntValue);
                        break;
                    case R.id.lts:
                        Punchinglts();
                        Toast.makeText(context, "Your Lunch Time Start From Now..", Toast.LENGTH_SHORT).show();
                         LS.setBackgroundColor(myIntValue);

                        break;
                    case R.id.lte:
                        Punchinglte();
                        Toast.makeText(context, "Your Lunch Time End Here..", Toast.LENGTH_SHORT).show();
                           LE.setBackgroundColor(myIntValue);

                        break;
                    case R.id.mrms:
                        Punchingmrms();
                        Toast.makeText(context, "Your MRM Time Start From Now..", Toast.LENGTH_SHORT).show();
                        MRMS.setBackgroundColor(myIntValue);
                       break;
                    case R.id.mrme:
                        Punchingmrme();
                        Toast.makeText(context, "Your MRM Time End Here..", Toast.LENGTH_SHORT).show();
                         MRME.setBackgroundColor(myIntValue);

                        break;
                    default:
                        break;
                }
            } 
Manishika
  • 5,478
  • 2
  • 22
  • 28

3 Answers3

0

Please see this video.

When doing the key-value pairs I would store integers and then translate them back into colors. For example, if I store "1" with the key "button1-color" then when using getString("button_color") translate this into a color, say

1 -> red
2 -> orange
3 -> yellow
4 -> green
5 -> blue
SnakeException
  • 1,148
  • 1
  • 8
  • 26
0
  1. Create a SharedPreference instance
mSharedPreferences = getSharedPreferences("shared_pref_name", MODE_PRIVATE);
  1. Add these methods in your Activity

    private void saveViewColor(int viewId, int color) {
        mSharedPreferences.edit().putInt(String.valueOf(viewId), color).apply();
    }

    private int getViewColor(int viewId) {
        return mSharedPreferences.getInt(String.valueOf(viewId), Color.parseColor("#FFFFFF"));
    }

    private void removeViewColor(int viewId) {
        mSharedPreferences.edit().remove(String.valueOf(viewId)).apply();
    }

    private void clearAll() {
        mSharedPreferences.edit().clear();
    }
  1. Invoke method when view click
view.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {


        int colorInt = Color.BLUE;

        Drawable drawable = v.getBackground();

        //get color from the view which being clicked
        if (drawable instanceof ColorDrawable) {
            colorInt = ((ColorDrawable) drawable).getColor();
        }

        int viewId = v.getId();

        v.setBackgroundColor(colorInt);

        //save color
        saveViewColor(viewId, colorInt);

        //get current view color
        getViewColor(viewId);

        //remove color for current view
        removeViewColor(viewId);

        //clear all SharedPreferences not only the color
        clearAll();
    }
});
Jeffery Ma
  • 3,051
  • 1
  • 23
  • 26
  • Hey @Magic ,any idea about this issue : https://stackoverflow.com/questions/56742838/how-do-i-retrieve-the-previous-fragment-upon-opening-an-app-from-background-ap –  Jun 24 '19 at 20:05
0

The way I would do that is create a new Preferences class to store all of your colours, then create getters and setters for each.

public class Preferences extends Activity {

public static final String PREF_FILE = "MyPrefsFileKey";

public int setColour1() {
        SharedPreferences prefs = getContext().getSharedPreferences(PREF_FILE, MODE_PRIVATE);

        colour = prefs.getInt("colour1Key", 0); //0 being the default value.

      return colour1;
    }

    public int getColour1(int colour1) {

        SharedPreferences pref = getContext().getSharedPreferences(PREF_FILE, MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();

        editor.putInt("colour1Key", colour1);
        editor.apply();
        return colour1;
    }
}

Now to save your colour, simply add this under each case. However you will need to create more getters and setters for each.

preferences.getColour1(getContext(), myIntValue1);

Remember to add a new instance of your Preferences class in the class you're saving from like this

Preferences preferences = new Preferences();

Let me know if you have any trouble.

-- EDIT apparently I cannot edit until I have 50 reputation. In your code you had .setBackgroundColor (myIntValue) myIntValue1 being the value of the first colour you're trying to save.