How can I change background color of all xml pages through three radio buttons, one is Red, another is Blue, and other is Green. When I will click onto one of them, then the background color of all XML pages will be changed into that selected color. Pls help me.
-
post your code here ....... – Quick learner May 19 '15 at 09:37
-
Do you mean that you want to change the backgrounds through a preferences(Settings) screen radio buttons – M090009 May 19 '15 at 09:39
-
you want to change whole theme of app or only the single screen where the three radio buttons are present????? – Amol Sawant May 19 '15 at 09:40
-
do it with custom theme set it dynamically by setTheme method. – Jignesh Jain May 19 '15 at 09:41
-
post some sample code what are you trying to do – Syed Raza Mehdi May 19 '15 at 09:41
-
I have just created an xml page which contain three radio buttons, namely RED, BLUE, GREEN. – ARIT May 19 '15 at 09:41
-
Do you mean to change all your layouts background to change on the radio button click or something ? or you want to change just the current layout where the radio button is ? In either case, on click of any of the radio buttons, save the preference of the color you want to change and on the onResume call for any of the layouts(With Activities), change the color of the background that you have saved in the preference. – mike20132013 May 19 '15 at 09:41
3 Answers
You should findViewById()
your root layout
and set within the method setBackgroundColor()
. And by switching RadioButton
you specify different color. See example below.
RelativeLayout rLayout = (RelativeLayout) findViewById(R.layout.the_id);
RadioButton rb = (RadioButton) findViewById(R.id.yourFirstRadioButton);
rb.setOnClickListener(listener);
and then:
OnClickListener listener = new OnClickListener (){
public void onClick(View v) {
switch(v.getId()){
case 0: //assume 0 is red
rLayout.setBackgroundColor(Color.parseColor(Color.RED));
break;
case 1: //assume 1 is blue
rLayout.setBackgroundColor(Color.parseColor(Color.BLUE));
break;
case 2: //assume 2 is green
rLayout.setBackgroundColor(Color.parseColor(Color.GREEN));
break;
}
}
}
If you want to change layout color in ALL Activities
you should remember the value and pass it through Intent
or declare static
variable with the current color.See examples:
In Activity
with RadioButton
s:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("color", green);
startActivity(intent);
And in NextActivity
you get it like this:
Bundle bundle = getIntent().getExtras();
if(bundle != null){
switch(bundle.getInt("color")){
case 0: // 0 was red
rLayout.setBackgroundColor(Color.RED);
break;
case 1: // 1 was blue
rLayout.setBackgroundColor(Color.BLUE);
break;
case 2: // 2 was green
rLayout.setBackgroundColor(Color.GREEN);
break;
}
}
With the static
variable I hope you know how to deal, so by switching RadioButton
's you assign the value and in another Activity
you check the value in switch/case
statement as I described above.
Another common way is saving value in SharedPreferences
. This is a good practice and it is light-weight, but also depends how many Activities
you have. If just 2 - doesn't make sense, if 10 - yes. Nice answer is given here.
That's it. Good luck.
do it with custom theme set it dynamically by setTheme() method check below link

- 1
- 1

- 1,518
- 12
- 28
This is one option:
You can save in SharedPreferences
the color you toggled. For example:
Create your SharedPreferences
reference:
SharedPreferences prefs = getActivity().getSharedPreferences( getActivity().getPackageName(),Context.MODE_PRIVATE);
Then when you select a color (for example blue) do:
prefs.edit().putInt("TAG", Color.Blue).apply();
Then on any Activity
onCreate
method (after the setContentView
call) or if its a fragment on onViewCreatedMethod
you do:
If Activity
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout."layout_id");
SharedPreferences prefs = getActivity().getSharedPreferences(getActivity().getPackageName(),Context.MODE_PRIVATE);
int backgroundColor = prefs.edit().getInt("TAG")
View rootView = findViewById(R.id."layout_id");
rootView.setBackgroundColor(backgroundColor);
}
if Fragment
:
public void onViewCreated(View view, Bundle savedInstanceState) {
SharedPreferences prefs = getActivity().getSharedPreferences(getActivity().getPackageName(),Context.MODE_PRIVATE);
int backgroundColor = prefs.edit().getInt("TAG")
view.setBackgroundColor(backgroundColor);
}
This way, when you load a page the color of its background will be changed to the one you've store in preferences.

- 15,802
- 10
- 54
- 91

- 128
- 10