How to permanently change application background color

I want to change the background color of my application so that whenever I want to change the color at runtime , I can just click the buttons in the application to change the background color. After changing the color, I want it to stay there when I open the application again. I would also like to know how to change the color of other actions while I am in another action. Thank you 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> 

+6
source share
2 answers

You can use Android Shared Preferences to remember the selected background color for the application. Therefore, every time you open the application, you can check the value of the general setting and apply the color accordingly.

Use a common base activity class, which all other actions will be received, and in the "OnCreate" and "OnResume" methods write code to read the general preference value and apply the back color. This method, when you open any selected background activity color will be applied.

Try it under the code, it is tested and works.

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(); } } 

Action 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); } } 

Color List Colors.xml

 <?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> 
+2
source

To do this, you can maintain a database for storing values ​​for different colors.

Now, when you click the button after selecting a specific color for your application, you just need to update the color value in your database.

In the onCreate () method of your MainActivity (Launcher Activity), you just need to get the color value from the database field and set it in your application.

+1
source

All Articles