fernando sor fernando sor - 2 months ago 7x
Android Question

setting window background with radio buttons

i have an application i am working on that has a radioButton group that changes the image background of my main activity. it is working well with one issue i cant seem to solve.

the background always defaults back to the holo background after i switch screens or resart the app. the radio buttons are set up to press properly with a selector xml in drawables and the images
switch flawlessly but just don't stick. also how would i spread this across all classes without recreating the radio buttons on every screen?

here is my code for the radio buttons

public class MainActivity extends Activity {

private final String TAG = "Main Activity";

Button rButton2;
Button rButton1;
Button rButton;
Button mButton;

protected void onCreate(Bundle savedInstanceState) {
final RelativeLayout ll=(RelativeLayout) findViewById(R.id.RelativeLayout);
rButton2 = (Button) findViewById(R.id.radio2);
rButton1 = (Button) findViewById(R.id.radio0);
rButton = (Button) findViewById(R.id.radio1);
rButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Log.i(TAG, "onStart");

rButton1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Log.i(TAG, "onStart");

rButton2.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Log.i(TAG, "onStart");


Save your preferences First you need to save your settings. Use SharedPreferences for that.

Second you need to set your bakground from code. Either add the initialization code to all your Activities or define a style.

Option1: Initialize background in activities. In the onCreate() of all your activities you call the setBackgroundResource method with the value you retrieve from the SharedPreferences.

Option2: Create themes. Create themes based on the Holo theme of your choice and set it in the onCreate() of your activities. Example for how you can do it is described in this post: How to change current Theme at runtime in Android

Here is a trick for theme switching: How do I restart an Android Activity (You should call recreate())