1

Hello I have four custom defined styles in styles.xml and I am trying to use a RadioGroup in order to select the overall style of the entire application.

<style name="blueTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
    <item name="android:windowBackground">@color/blue_background</item>
    <item name="android:textColorPrimary">@color/blue_text</item>
</style>
<style name="redTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
    <item name="android:windowBackground">@color/red_background</item>
    <item name="android:textColorPrimary">@color/red_text</item>
</style>
<style name="greenTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
    <item name="android:windowBackground">@color/green_background</item>
    <item name="android:textColorPrimary">@color/green_text</item>
</style>
<style name="bwTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
    <item name="android:windowBackground">@color/bw_background</item>
    <item name="android:textColorPrimary">@color/bw_text</item>
</style>

Here is the code for my actual handling of the RadioGroup selection, I previously had attempted this with CheckBoxes that's why the names are all checkboxes.

blueCheckBox = (RadioButton) findViewById(R.id.blueCheckBox);
    redCheckBox = (RadioButton) findViewById(R.id.redCheckBox);
    greenCheckBox = (RadioButton) findViewById(R.id.greenCheckBox);
    bwCheckBox = (RadioButton) findViewById(R.id.bwCheckBox);

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.themeRadioGroup);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected

            switch(checkedId) {
                case R.id.blueCheckBox:
                        getApplication().setTheme(R.style.blueTheme);
                    break;
                case R.id.redCheckBox:
                    getApplication().setTheme(R.style.redTheme);
                    break;
                case R.id.greenCheckBox:
                    getApplication().setTheme(R.style.greenTheme);
                    break;
                case R.id.bwCheckBox:
                    getApplication().setTheme(R.style.bwTheme);
                    break;
            }
        }
    });

The problem I am having though is that the overall style simply will not change. In my AndroidManifest.xml file I defaulted my theme to

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/blueTheme" >

but despite that, when i run my app the theme is not blueTheme. blueTheme does not have the colors that are displayed currently on all my app's activities. for some reason the theme being displayed has gray text and a dark gray / blackish background, the only colors that are properly displayed are the background colors of my buttons which I manually set in each individual layout/activity_myActiviitesThatHaveButtons xml file. I am not sure what I am doing wrong, can anyone please try to provide some assistance? Thank you for your help

P.S: Not sure if this matters but I believe my min API is set to 8, I believe that is what my professors wants it set as.

Biggytiny
  • 519
  • 10
  • 29

1 Answers1

0

Create a base activity which overrides onCreate and sets the theme for the activity based on a preference. Then make all your activities override this preference. The preference will be set when the user changes their selection on the radio button.

More information can be found on a similar question answered before:

Switching application-wide theme programmatically?

Community
  • 1
  • 1
TomRichardson
  • 5,933
  • 5
  • 27
  • 30