1

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;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);             
      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");
                ll.setBackgroundResource(R.drawable.background1);
          }
        });

      rButton1.setOnClickListener(new OnClickListener() {     

          public void onClick(View v) {
                Log.i(TAG, "onStart");
                ll.setBackgroundResource(R.drawable.background);    
          }
        });

      rButton2.setOnClickListener(new OnClickListener() {     

          public void onClick(View v) {
                Log.i(TAG, "onStart");
                ll.setBackgroundResource(R.drawable.background2);   
          }
        }); 
}
Ravi
  • 34,851
  • 21
  • 122
  • 183

1 Answers1

2

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

Community
  • 1
  • 1
allprog
  • 16,540
  • 9
  • 56
  • 97
  • thanks for answer so if i created a theme for each radio button in styles I could just set my image background there right? – fernando sor Aug 30 '13 at 14:20
  • yes, but setting the theme is a little tricky. If you encounter issues, then just make a search on it, there are plenty of answered questions. Also, keep in mind the note from the documentation: http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme(int) Yom must call setTheme() before creating any views, that is, before the setContentView() call. Additionally, this may not work for already created activities but the next time you start the activity, it will be activated. – allprog Aug 30 '13 at 15:05
  • haha thanks. I always wondered what people meant by accept the answer. – fernando sor Aug 30 '13 at 18:18