-2

Image error I have 2 styles files. With a click on a button I want to switch from AppTheme to AppTheme2. how to make?

Note that the primary color is with a code, colorPrymary2. That would change the color of my app completely, and that's what I want.

Manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat">
        <item name="android:windowBackground">@drawable/background</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme2" parent="Theme.AppCompat">
        <item name="android:windowBackground">@drawable/background</item>
        <item name="colorPrimary">@color/colorPrimary2</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources> 
user1506104
  • 6,554
  • 4
  • 71
  • 89
  • 3
    Possible duplicate of [How to change current Theme at runtime in Android](https://stackoverflow.com/questions/2482848/how-to-change-current-theme-at-runtime-in-android) – Shubham AgaRwal Jul 28 '18 at 14:37

1 Answers1

1

Just do it in onCreate() method!

getApplication().setTheme(R.style.yourtheme);
Gourango Sutradhar
  • 1,461
  • 10
  • 16