Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 311
programmingandroid Avatar asked Oct 23 '15 11:10

programmingandroid


People also ask

How do I change the background color of my apps?

Select Start > Settings > Personalization > Colors, and then choose your own color, or let Windows pull an accent color from your background.

How do I change the default button color?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.


Video Answer


2 Answers

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>
like image 158
Deshan Avatar answered Nov 07 '22 09:11

Deshan


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.

like image 20
Jaimin Modi Avatar answered Nov 07 '22 10:11

Jaimin Modi