Changing the theme of an android app using preferences
<p>I have been trying to get my app to read data from the preferences, and change the theme according to the option selected. I have found many different suggestions on the internet, including here, but have been unable to get it to work.</p>

<p>I have created preferences.xml and arrays.xml, and the user is able to select the theme they want. However, the change is not reflected in the app.</p>

<p>Here are the contents of ActivityMain.java:</p>

<pre><code>public class MainActivity extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userTheme = preferences.getString("prefTheme", "darkab"); 
    if (userTheme.equals("darkab"))
        setTheme(R.style.darkab);
    else if (userTheme.equals("light"))
        setTheme(R.style.light);
    else if (userTheme.equals("dark"))
        setTheme(R.style.dark);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@SuppressLint("NewApi")
protected void onResume(Bundle savedInstanceState) {
    recreate();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userTheme = preferences.getString("prefTheme", "darkab");
    if (userTheme.equals("darkab"))
        setTheme(R.style.darkab);
    else if (userTheme.equals("light"))
        setTheme(R.style.light);
    else {setTheme(R.style.dark);}
    super.onResume();
    setContentView(R.layout.activity_main);
}
</code></pre>

<p>These are the styles I wish to use, as set in styles.xml:</p>

<pre><code>&lt;style name="darkab" parent="android:Theme.Holo.Light.DarkActionBar"&gt;&lt;/style&gt;
&lt;style name="light" parent="android:Theme.Holo.Light"&gt;&lt;/style&gt;
&lt;style name="dark" parent="android:Theme.Holo"&gt;
</code></pre>

<p>And here is my preferences.java file:</p>

<pre><code>public class Preferences extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}
</code></pre>

<p>Any help would be appreciated.</p>
