1

I'm trying to do a settings activity in my app, and I was wondering how can I do a preference that allow the user to change the background color.

I have already done the settings activity, and I was thinking of doing a subsetting when the user clicks on the "Color" option, that shows various colors that the user can set (or, even better, with a palette with all the colors available).

How can I realize that?

Zong
  • 6,160
  • 5
  • 32
  • 46
Solaire
  • 53
  • 1
  • 8

1 Answers1

1

well if you have already created settings activity, it would be something similar to this

public class NormalSettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.normal_preferences);
    }
}

in your preferences xml add preference category like this

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Background Color Settiongs" >
        <ListPreference
            android:defaultValue="#111111"
            android:entries="@array/colorName"
            android:entryValues="@array/colorCode"
            android:key="background_color"
            android:summary="Set background color of app"
            android:title="Colors" />
    </PreferenceCategory>
</PreferenceScreen>

then in values folder create a xml called arrays

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="colorName">
        <item name="0">Red</item>
        <item name="1">Black</item>
        <item name="2">Yellow</item>
        <item name="3">White</item>
    </string-array>
    <string-array name="colorCode">
        <item name="0">#ff0000</item>
        <item name="1">#111111</item>
        <item name="2">#ffff33</item>
        <item name="3">#ffffff</item>
    </string-array>
</resources>

then in your activity, simply set background color using

backgroundLayout.setBackgroundColor(Color.parseColor(mPreferenceManager.getDefaultSharedPreferences().getString("background_color", "#111111")));

here "background_color" is coming from preferences xml (android:key="background_color")

"#111111" is some default color that will be set if no match found

don't forget to create a global variable

protected PreferencesManager mPreferenceManager;

and initialize it in onCreate, as

mPreferenceManager = PreferencesManager.instance(this);
Kaustuv
  • 811
  • 5
  • 9
  • I don't know how to thank you, your answer was more than complete, thank you very mutch. I just wanna point out that the final procedure to make the background work, didn't work for me, so I did this ' RelativeLayout rl = (RelativeLayout)findViewById(R.id.container); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); int bgc = Color.parseColor(sharedPref.getString("background_color", "#000000")); rl.setBackgroundColor(bgc); ' The problem is that the background is not applied if I go back with the arrow out of the screen, from the settings – Solaire May 09 '14 at 13:13
  • i faced the very same problem. the trick here is to call setbackground method in onResume method of activity. and to thank me you can always accept my answer :-) – Kaustuv May 09 '14 at 13:52
  • Oh right, the answer was so obvious! Thank you very much, I accept it right away ;) – Solaire May 09 '14 at 15:02