Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change to a Holo Dark/Light themes from within the app?

Tags:

android

I know it's possible to have it so if I have a setting I can change between Holo.Light and Holo, however, I cannot seem to find out how. All help is appreciated!

like image 205
ollien Avatar asked Dec 02 '12 08:12

ollien


2 Answers

I think that you can do it by using the setTheme() method. Just make sure that you call it before you use setContentView, or it won't work.

For example:

if(userChoice ==1){
   setTheme(android.R.style.Theme_Holo_Light);
else if(userChoice == 2){
    setTheme(android.R.style.Theme_Holo);
}

A list of themes can be found here

like image 80
jcw Avatar answered Sep 21 '22 12:09

jcw


As per a comment on the answer posted, if you need to toggle between the default Holo Themes, use this:

if (mThemeId == R.style.AppTheme.Dark) {
        mThemeId = android.R.style.Theme_Holo_Light;
    } else {
        mThemeId = android.R.style.Theme_Holo;
    }
this.recreate();

To use your own custom defined themes from your Styles.XML file. For example, something like this:

<style name="ActionBar" parent="@android:style/Widget.Holo.ActionBar" />

<style name="ActionBar.Light" parent="@style/ActionBar">
    <item name="android:background">@color/actionbar_background_light</item>
</style>

<style name="ActionBar.Dark" parent="@style/ActionBar">
    <item name="android:background">@color/actionbar_background_dark</item>
</style>

<style name="AppTheme.Light" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBar.Light</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="listDragShadowBackground">@android:color/background_light</item>
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_light</item>
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_light</item>
    <item name="menuIconShare">@drawable/ic_menu_share_holo_light</item>
</style>

<style name="AppTheme.Dark" parent="@android:style/Theme.Holo">
    <item name="android:actionBarStyle">@style/ActionBar.Dark</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="listDragShadowBackground">@android:color/background_dark</item>
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_dark</item>
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_dark</item>
    <item name="menuIconShare">@drawable/ic_menu_share_holo_dark</item>
</style>

Define this as a Global Variable in your Activity:

private int mThemeId = -1;

And set your onCreate() method like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState != null) {
        if (savedInstanceState.getInt("theme", -1) != -1) {
          mThemeId = savedInstanceState.getInt("theme");
          this.setTheme(mThemeId);
        }
        mTitlesHidden = savedInstanceState.getBoolean("titlesHidden");
    }

    setContentView(R.layout.main);
}

And the code to toggle between the two themes:

if (mThemeId == R.style.AppTheme.Dark) {
    mThemeId = R.style.AppTheme.Light;
} else {
    mThemeId = R.style.AppTheme.Dark;
}
this.recreate();

Note: The theme has to be set before your call to setContentView()

like image 34
SSL Avatar answered Sep 23 '22 12:09

SSL