0

I want to change the manifest style when the user chooses a setting in the preferences file or settings. I have a setting in my apk which includes a list of preferences with three entries {Style_1, Style_2, Style_3}. I want to change the style color, like primaryColor, when the user clicks a style. How can I do this?

user812786
  • 4,302
  • 5
  • 38
  • 50
Azhy
  • 704
  • 3
  • 16
  • yes, you can apply the theme in oncreate method programmatically but you need to recreate the activity to which you apply new theme – Usman Rana Jul 05 '17 at 09:43

4 Answers4

2

Yes you can do this easily, I do it all the time.

Just call this method before setContentView like this:

 setTheme(R.style.Theme);
 setContentView(R.layout.activity_layout);

Now what I do is that i take a static int variable in the app constants and change it according to my theme. Then i do something like this

 //This is in my constants file
 public static int CURRENT_THEME = R.style.AppTheme;

 //This is in my onCreate of every Activity.
 setTheme(Constants.CURRENT_THEME);
 setContentView(R.layout.activity_layout);

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
1

Yes you can set Theme like this:

    activity.setTheme(R.style.theme_large);
    activity.setTheme(R.style.theme_small);

    <style name="theme_large">
            <item name="main_background">@drawable/background_red</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="button_light">@color/button_light</item>
    </style>



    <style name="theme_small">
            <item name="main_background">@drawable/background_red</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="button_light">@color/button_light</item>
    </style>
1

Create Two Style

 setTheme(darkTheme ? R.style.AppThemeDark : R.style.AppThemeLight);

<style name="AppThemeDark">
            <item name="main_background">@drawable/background_dark</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="button_light">@color/button_dark</item>
    </style>



<style name="AppThemeLight">
        <item name="main_background">@drawable/background_light</item>
        <item name="colorPrimaryDark">@color/colorPrimaryLight</item>
        <item name="button_light">@color/button_light</item>
</style>
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

I think, setting theme after a button click or option choosen could be harsh for an Activity to handle. What I am suggesting you is to reload your activity on button click but before that simply save the style_name , the user want to apply in internal memory or Shared Preferences. You can apply shared preference simply looking at here.

At the start of onCreate of your activity, apply that fetching part of shared preferences and apply the theme as user has indicated. This would help you to preserve the theme for that user until s/he uninstall that application or clears the app data. If s/he is using the application for first time, the stored style_name string would be null so load your application with default theme.

You can simply reload your activity on button click by using the below code:

public void onClick (View v){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

Hope it helps !!

Rishav Chudal
  • 171
  • 1
  • 8