11

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

Community
  • 1
  • 1
Giorgi
  • 30,270
  • 13
  • 89
  • 125

1 Answers1

5

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()
        }
    }
}
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • hello sherif, I wanted to know from you if there could be any solution where I get color code from web and according I can change color of all my buttons runtime without going to all particular buttons and applying BackGround color, could there be any solution using theme or style? Please share any suggestion you have. – MKJParekh Mar 20 '14 at 07:54
  • Do you have infinite colors? or just a limited set of colors? – Sherif elKhatib Mar 21 '14 at 15:15
  • I do have limited 20 colors for say, but those 20 gets downloaded from server so it may vary from one time to another, in short colors are around 15-20(not fix) and those color codes are also not fix. One of the user has suggested to use CustomViews http://stackoverflow.com/questions/22529646/android-app-apply-color-theme-dynamically-at-runtime – MKJParekh Mar 22 '14 at 04:40