Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change and apply theme at runtime in Android [duplicate]

Possible Duplicate:
How to change current Theme at runtime in Android

I have an Android application where I allow users to switch between themes at runtime. Switching a theme is easy but the theme isn't applied until the activity is recreated. I found a way to apply the theme to current activity but if the user presses back button previous screens still have the old theme. How can I change theme for those activities? Example of app that supports it: Tasks Free

like image 842
Giorgi Avatar asked Jul 10 '12 20:07

Giorgi


People also ask

How can I change my android theme?

Settings. Under Display Options, tap Theme. Select the theme for this device: Light—White background with dark text.

How is an android theme applied to the whole application?

You can create a theme the same way you create styles. The difference is how you apply it: instead of applying a style with the style attribute on a view, you apply a theme with the android:theme attribute on either the <application> tag or an <activity> tag in the AndroidManifest. xml file.

How do I change the default theme in Android Studio?

To change default themes go to File and click on Settings. A new Settings dialog will appear, like this. Under the Appearance & Behaviour -> Appearance, you will find Theme. Choose the right theme from the drop-down and click on Apply and then Ok.


2 Answers

Dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView(). To change the theme, you simply need to restart your activity.

Please see this file..!

Also Want see this and this ... Hope this helps...!

like image 52
Strider Avatar answered Oct 27 '22 07:10

Strider


Just a hint I suppose:

Before finish(); Call

setResult(AnIntegerThatNotifiesThePreviousActivitiesToChangeTheme);

Now in all your Activities, implement onActivityResult

protected void onActivityResult(int request, int result, Intent data) {
    if(result == AnIntegerThatNotifiesThePreviousActivitiesToChangeTheme)
    {
        //update the current theme
    }
}

Another solution (Better):

Implement a class that saves the theme:

public class CurrentThemeHolder {
    private CurrentThemeHolder() {
    }
    private static instance;
    public static getInstance() {
        if(instance == null)
            return new CurrentThemeHolder();
        else
            return instance;
    }
    private int mTheme; //identifier of the theme
    public getTheme() {
        return mTheme;
    }
    public setTheme(int newTheme){
        mTheme = newTheme;
    }
}

Now let all ur activities extend this ThemeActivity:

public class ThemeActivity extends Activity {
    private int mTheme;
    protected void onResume() {
        if(mTheme != CurrentThemeHolder.getInstance().getTheme()) {
            //do what you should do to set the theme
            mTheme = CurrentThemeHolder.getInstance().getTheme();
            //everytime you set the theme save it
            //this maybe should be done in onCreate()
        }
    }
}
like image 38
Sherif elKhatib Avatar answered Oct 27 '22 06:10

Sherif elKhatib