To change the topic "on the fly" you need to do quite tedious manipulations.
Usually all this happens in the settings.
Through the setTheme()
method, you can set the desired topic in each activation, the changes will take effect after the restart of the activation (return from settings), in which there is a change of topic. The method must be used BEFORE the setContentView()
method.
The current selected theme is best stored in SharedPreferences
, writing the value from the settings there, and when starting, activate it from there.
public void onCreate(Bundle savedInstanceState) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); int theme = sp.getInt("THEME", R.style.AppTheme); setTheme(theme); super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); }
where the value of the key "THEME", which will be recorded in the settings, is equal to the ID of the selected theme specified in res / styles.xml , so, for an example with styles below, these will be the values: R.style.AppTheme
and R.style.AppThemeLight
UPD
For themes based on AppCompat
themes are not fully applied, to solve this problem, you must specify the colors ColorPrimary
, ColorPrimaryDark
and ColorAccent
in the styles.xml
with the themes that will change in the application:
styles.xml
<resources> <style name="AppThemeLight" parent="@style/Theme.AppCompat.Light"> <item name="colorPrimary">@color/primary_light</item> <item name="colorPrimaryDark">@color/primary_dark_light</item> <item name="colorAccent">@color/accent_light</item> </style> <style name="AppTheme" parent="@style/Theme.AppCompat"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primary_dark</item> <item name="colorAccent">@color/accent</item> </style> </resources>
We define the colors themselves (you can choose your own ones, which ones you like) in colors.xml :
<resources> <color name="primary_light">#edeceb</color> <color name="accent_light">#517c50</color> <color name="primary_dark_light">#0d0c0c</color> <color name="primary">#2d2c2a</color> <color name="accent">#a5c0df</color> <color name="primary_dark">#e2e0e0</color> </resources>
For themes based on Holo
, you also need to specify some parameters so that there are no problems:
styles.xml
<resources> <style name="AppThemeLight" parent="android:Theme.Holo.Light"> <item name="android:background">#FFFFFF</item> <item name="android:textColor">#000000</item> <item name="android:actionBarStyle">@style/LightActionBar</item> </style> <style name="AppTheme" parent="android:Theme.Holo"> <item name="android:background">#000000</item> <item name="android:textColor">#FFFFFF</item> <item name="android:actionBarStyle">@style/BlackActionBar</item> </style> <style name="LightActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"> <item name="android:background">#FFFFFF</item> </style> <style name="BlackActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:background">#000000</item> </style> </resources>
PS: Guaranteed application of the theme will occur when the application is restarted (and in general when the activation is restarted, which does not always happen, since they are not necessarily destroyed during the transitions)