user10013758
user10013758

Reputation:

how to let the user decide a particular color for a button

Code

Button mButton;
int mDefaultColor;

mButton = (Button)findViewById(R.id.buttontextcolorsent);
    mDefaultColor = ContextCompat.getColor(Customise.this,R.color.white);
    mButton.setBackgroundColor(mDefaultColor);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openColorPicker();
        }
    });

}

private void openColorPicker() {
    AmbilWarnaDialog colorPicker = new AmbilWarnaDialog(this, mDefaultColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
        @Override
        public void onCancel(AmbilWarnaDialog dialog) {

        }

        @Override
        public void onOk(AmbilWarnaDialog dialog, int color) {

            mDefaultColor = color;
            mButton.setBackgroundColor(mDefaultColor);
        }
    });
    colorPicker.show();
}

}

This is a popular library called ambil warna which shows a colorpicker... when a user picks the color it successfully changes the color of the button... but the problem is its not permanent... like if the user exits and enters the activity again the button will turn back to its original default color... so how do i make this change permanent? and i dont want to use any kind of server database... so is it possible to make this change using SQLlite? or is there any other way?

Upvotes: 0

Views: 63

Answers (1)

Tobias
Tobias

Reputation: 7415

You could just store the selected color to SharedPreferences.

 PreferencaManager.getDefaultSharedPreferences()
   .edit()
   .putInt("button_color", newHighScore)
   .apply()

Upvotes: 1

Related Questions