user1579664
user1579664

Reputation: 15

how to persist colour of text in edittext?

I have an activity which has count buttons plus and minus such that it increments or decrements the value in editText, a non negative no. such that int value is set to colour by setTextColor property.The value persists through Shared Preference.My question is how to persist that 'coloured' green or red value in the editText when again entering in the app?

Upvotes: 0

Views: 168

Answers (1)

Sam
Sam

Reputation: 86958

Let's assume your EditText is named mEditText. First set up a SharedPreferences object:

mSharedPreferences = getSharedPreferences("Preferences File Name", MODE_PRIVATE);

Save the color (possibly in onDestroy() or whenever the color is changed):

SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt("Text Color", mEditText.getTextColors().getDefaultColor());
editor.commit();

Read the saved value with a black default value if there is no saved data (possibly in onCreate()):

mEditText.setTextColor(mSharedPreferences.getInt("Text Color", 0xff000000));

Upvotes: 1

Related Questions