Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change PreferenceActivity theme?

Tags:

java

android

I'm trying to change the theme for the PreferenceActivity in my app and I just can't get it to work.

This is the xml:

    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

        <SwitchPreference android:title="Auto Clear" android:key="autoclear" android:summary="Clear the command line when the code is being executed." android:defaultValue="false"/>
        <ListPreference android:title="Choose a theme" android:negativeButtonText="" android:dialogTitle="" android:key="theme" android:entries="@array/themesList" android:entryValues="@array/themesList" android:defaultValue="Default" />

</PreferenceScreen>

And this is the PreferenceActivity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    this.setTheme(R.style.AppTheme);

    addPreferencesFromResource(R.xml.preferences);

}

And the result is:

Result

like image 442
Eli Revah Avatar asked Aug 01 '12 01:08

Eli Revah


People also ask

Is it possible to change the theme of an activity?

That being said, it is possible to change the theme of an Activity, however only in the`onCreate` method and only before `super` is called. This is problematic because it’s hard to provide a seamless experience to the user if you have to restart the app or Activity in order to change the theme.

How do I change the theme in OFM?

Click the arrow to the left of the Visual Design folder to expand the selection. Select Theme Settings. In the Select Theme field, use the drop down to select your theme. At this time, OFM will continue to provide instruction using the SAP Signature theme.

Can I set the views theme when I build an activity?

An alternate solution can be to set the Views theme related attributes when we build our Activity, Fragment or Layout. This will still add to the complexity of your code. There is a time and performance cost to doing this for each layout. There is a third option?

How do I change the default theme in Windows 10?

Open the Settings app. Go to Personalization -> Themes. On the right, click on a theme you like under Apply a theme. The theme is now applied. Alternatively, you can use the classic Control Panel applet to quickly apply a custom theme, or any of the default themes, or the High Contrast theme. Press the Win + R keys on the keyboard.


2 Answers

Have you tried applying the theme on the activity tag in the manifest? This is how I have done it before -

<activity 
  android:label="@string/app_name" 
  android:name="com.example.MyPreferenceActivity"
  android:theme="@android:style/Theme.Black"
  android:exported="true"
  android:icon="@drawable/ic_launcher"></activity>

EDIT:

The other option you could try is to override onApplyThemeResource(Resources.Theme theme, int resid, boolean first). Looking at the android source code the setTheme will internally call method.

/**
 * Called by {@link #setTheme} and {@link #getTheme} to apply a theme
 * resource to the current Theme object.  Can override to change the
 * default (simple) behavior.  This method will not be called in multiple
 * threads simultaneously.
 *
 * @param theme The Theme object being modified.
 * @param resid The theme style resource being applied to <var>theme</var>.
 * @param first Set to true if this is the first time a style is being
 *              applied to <var>theme</var>.
 */
protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
    theme.applyStyle(resid, true);
}
like image 183
shri046 Avatar answered Sep 21 '22 14:09

shri046


At last i found out how to change theme of "PreferenceActivity" programmatically(via java code)

To change theme just do like this:

        @Override
        public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Holo_Theme_Light);
        super.onCreate(savedInstanceState);
        }

Always call setTheme(R.style.yourtheme); method before super.onCreate(savedInstanceState); method. By doing this it will produce result as shown below.

enter image description here

That's all.

If yo call setTheme(R.style.yourtheme); method after super.onCreate(savedInstanceState); method it will produce result as shown below.

enter image description here

Note: Themes are not recognize by nested PreferenceScreen. To apply theme to that nested PreferenceScreen you have to make an another PreferenceActivity for that nested PreferenceScreen and call setTheme(R.style.yourtheme); method there.

like image 23
E Player Plus Avatar answered Sep 22 '22 14:09

E Player Plus