programmingandroid
programmingandroid

Reputation: 340

How to change background color of app permanently

I want to be able to change the background color of my app so that whenever i want to change the color during runtime, i can just press a button in the app to change the background color. After the changing the color i want that it should stay there whenever i open the app again. Also i would like to know how to change the color of other activities while I am in another activity. Thanks in advance. my colors.xml:

<resources>
<color name="original">#25383C</color>
<color name="grey">#484849</color>
<color name="red">#881A27</color>
<color name="orange">#ffa500</color>
<color name="yellow">#CDE707</color>
<color name="green">#00ff00</color>
<color name="aqua">#00FFCC</color>
<color name="marine">#0C0C84</color>
<color name="purple">#630A86</color>
<color name="silver">#c0c0c0</color>

styles.xml(it has themes):

<resources>
<style name="original">
    <item name="android:background">#25383C</item>
</style>
<style name="grey">
    <item name="android:background">#484849</item>
</style>
<style name="red">
    <item name="android:background">#881A27</item>
</style>
<style name="orange">
    <item name="android:background">#ffa500</item>
</style>
<style name="yellow">
    <item name="android:background">#CDE707</item>
</style>
<style name="green">
    <item name="android:background">#00ff00</item>
</style>
<style name="aqua">
    <item name="android:background">#00FFCC</item>
</style>
<style name="marine">
    <item name="android:background">#0C0C84</item>
</style>
<style name="purple">
    <item name="android:background">#630A86</item>
</style>
<style name="silver">
    <item name="android:background">#c0c0c0</item>
</style>

Upvotes: 6

Views: 2185

Answers (2)

Deshan
Deshan

Reputation: 2132

You can use Android Shared Preferences to remember the selected background color for the app.So every time you open the app you can check the value of the shared preference and apply the color accordingly.

Use a common base activity class that all the other activities will derive and in the "OnCreate" and "OnResume" methods write the code to read shared preference value and apply back ground color.This way when you open any activity selected background color will be applied.

Try below code, it is tested and working.

BaseActivity Class

 public class BaseActivity extends ActionBarActivity {

        private static final String PREFS_NAME="color_settings";
        SharedPreferences prefsReader = null;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            prefsReader=getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        }

        @Override
        public void setContentView(int layoutResID) {
            super.setContentView(layoutResID);
            setBackgroundColor();
        }

        protected void setBackgroundColor()
        {
            int background_resource_id= prefsReader.getInt("background_resource_id",0);
            View bgView= findViewById(R.id.main_container);
            bgView.setBackgroundColor(getResources().getColor(background_resource_id));
        }
        protected void setCurrentBackgroundColor(int colorResourceId)
        {
            SharedPreferences.Editor editor=getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
            editor.putInt("background_resource_id", colorResourceId);
            editor.commit();
        }
    }

Activity Class

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //save the color resource value in shared pref
        setCurrentBackgroundColor(R.color.red);

        setContentView(R.layout.activity_main);


    }


}

Colors.xml color list

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>
</resources>

Upvotes: 2

Jaimin Modi
Jaimin Modi

Reputation: 1667

For doing such thing you can maintain a database to store the values for the different colors.

Now, Whenever you press the button after choosing the particular color for your application you just have to update the color value in your database.

In onCreate() method of your MainActivity(Launcher Activity), You just have to retrieve the color value from the field of database and can set it in your application.

Upvotes: 1

Related Questions