0

I've seen this done before in Android so I know it's possible. I want to add a preference that changes the app theme using a listPreference. I've been fiddling around with some XML code but I'm stuck on the Java part.

Here is the listPreference in my settings.xml file:

<ListPreference
        android:entries="@array/Theme"
        android:entryValues="@array/themeAlias"
        android:key="theme_chooser"
        android:title="Theme"
        android:summary="Change the theme of the main activity." />

And here is my string arrays in my array.xml file:

<string-array name="Theme">
        <item>Default Theme</item>
        <item>Holo Light</item>
</string-array>
<string-array name="themeAlias">
        <item>"default"</item>
        <item>"light"</item>
</string-array>

Now here is some psuedocode that I don't know how to write into actual code:

Find the listPreference
   if preference is equal to default
       set the main activity's theme to custom a theme
   else
       set the theme to a standard light theme

I have done about 20 Google searches for this and all I get is a bunch of theming apps and blog posts about how to theme your android phone like a boss...I want to have users theme my app like a boss!

Thanks in advance,

Andrew

Andrew Quebe
  • 2,263
  • 5
  • 25
  • 53

1 Answers1

1

Check out this answer which recommends setting the theme for a base activity, which is extended by all your app's activities.

Community
  • 1
  • 1
Mike Ortiz
  • 4,031
  • 4
  • 27
  • 54
  • I'm not sure I understand what the answer means. I've been sitting here for about 2 hours messing with different code and nothing works. Could you explain his answer maybe? I've already overrode the onCreate() and all I'm trying to do is set the theme of the main activity based on what the user selects in the listpreference. – Andrew Quebe Jan 05 '14 at 11:41
  • Instead of extending Activity, your MainActivity and every activity in your app should instead extend MyBaseActivity, your own custom activity. In that activity's onCreate, call setTheme with the theme selected by the user. – Mike Ortiz Jan 05 '14 at 11:47
  • OK, I set up the new activity available [here](http://pastebin.com/vpnnWjVt). And changed main activity like [this](http://pastebin.com/p2cFSDsX). Now when I run this, it sets the theme to Holo Light and when I go in to change it, it does nothing. Solution? – Andrew Quebe Jan 05 '14 at 12:01
  • According to Jitesh's answer below, the theme must also be set before `super.onCreate()` is called. In MyBaseActivity, try also moving `super.onCreate()` to the end of the onCreate method. – Mike Ortiz Jan 05 '14 at 12:08
  • Still does not work. I feel I'm close and, as I said, I've tried everything! If this helps, [here](https://play.google.com/store/apps/details?id=com.oligon.lumistic) is a great example of what I'm trying to do. You'll have to download the app first and go into the settings where it says theme. This example works perfectly but I have no idea as to how the developer made this happen. I've tried debugging this app but have gained zero information from that. – Andrew Quebe Jan 05 '14 at 12:11
  • Can you confirm 1) that you are receiving the expected theme from the PreferencesManager and 2) that you can call `setTheme(android.R.style.Theme_Holo_Light)` on a single Activity and the theme will be switched correctly. Last, also note that onCreate must be called for the Activity to display the theme. So, if you change the theme, then press back to return to MainActivity, MainActivity's theme will not be changed, because its onCreate method has not been called. – Mike Ortiz Jan 05 '14 at 12:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44551/discussion-between-andrew-q-and-mike-ortiz) – Andrew Quebe Jan 05 '14 at 12:25