Jon mc cry
Jon mc cry

Reputation: 1

Android java Save Color

I have a button which has a default color, when I click on it it changes to yellow. Code looks like this.

  public void onClick(View v) {
        if (but[0] == 1) {
            ((Button) android.findViewById(R.id.pageOne)).setBackground(getResources().getDrawable(R.drawable.border_white));
        } else {
            ((Button) android.findViewById(R.id.pageOne)).setBackground(getResources().getDrawable(R.drawable.border_yellow));
        }
    }

Now I want this to be saved in the app. So when I restart that the button still shows yellow instead of the defaults. How to accomplish that?

Upvotes: 0

Views: 85

Answers (2)

You open the Shared Preferences:

 SharedPreferences sharedprefs = getSharedPreferences("yourapplicationpackage", MODE_PRIVATE);

and save the color:

 sharedprefs.edit().putString("buttoncolor", getResources().getDrawable(R.drawable.border_yellow)).apply();

You can get your color with:

Color buttoncolor= sharedprefs.getString("buttoncolor, "defaultcoloryoulike");

Good luck

Upvotes: 1

StephenG
StephenG

Reputation: 2881

You need to use the SharedPreferencesfeature of Android.

http://developer.android.com/reference/android/content/SharedPreferences.html

Upvotes: 1

Related Questions