Search code examples
androidandroid-activityandroid-xmlandroid-theme

How do I change the Activity's theme using styles.xml at runtime


There are 3 different types of style in the styles.xml like the following:

<style name="Theme_A" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#01AC50</item>
    <item name="colorPrimaryDark>#FF007838</item>
    <item name="colorAccent">#009688</item>"
<style>

The users are able to choose which style they want using the buttons, how can I change the whole Activity's style after clicking on the button?


Solution

  • How to setTheme to an activity at runtime? It doesn't work call setTheme before onCreate and setContentView has the suggestion to use

    setContentView(...);
    setTheme(R.style.MyTheme);
    setContentView(...);
    

    That is: it seems you need to setContentView after setTheme, as

    [setTheme] should be called before any views are instantiated in the Context