Skip to content
Advertisement

Runtime Change Android material theme

I have one setting screen for change app themes at runtime. I know how to create material design theme. I have create one in my style.xml file

Here is code of my style.xml:

 <style name="AppTheme" parent="AppTheme.Base"/>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/primaryBackground</item>
        <item name="colorPrimaryDark">@color/primaryBackground</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorControlNormal">@color/primaryBackground</item>
        <item name="android:colorControlActivated">@color/primaryBackground</item>
        <item name="android:colorControlHighlight">@color/primaryBackground</item>
        <item name="android:textColorPrimary">@color/primaryBackground</item>
        <item name="android:textColorSecondary">@color/primaryBackground</item>
        <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
        <item name="android:textCursorDrawable">@drawable/cursor_indicator</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

Now I want to change runtime app theme from green to purple or yellow. Anything can any one tell me how can I create a color picker from theme selection and how can I create multiple themes in my style.xml for change it to runtime.

Advertisement

Answer

Dynamic themes using style.xml

Here is my code

Style.xml

<resources>

    <style name="AppTheme.Base.Green" parent="AppTheme.Green">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.Base.Green.Dark" parent="AppTheme.Green">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg_black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.Base.Purple" parent="AppTheme.Purple">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.Base.Purple.Dark" parent="AppTheme.Purple">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg_black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <!-- Base application themes. -->
    <style name="ThemeApp.Green" parent="AppTheme.Base.Green"/>
    <style name="ThemeApp.Green.Dark" parent="AppTheme.Base.Green.Dark"/>
    <style name="ThemeApp.Purple" parent="AppTheme.Base.Purple"/>
    <style name="ThemeApp.Purple.Dark" parent="AppTheme.Base.Purple.Dark"/>


    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>


</resources>

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme.Green" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/primary_green</item>
        <item name="colorPrimaryDark">@color/primary_dark_green</item>
        <item name="colorAccent">@color/accent_green</item>
        <item name="colorControlHighlight">@color/primary_green</item>
        <item name="android:textColorPrimary">@color/primary_text</item>
        <item name="android:textColorSecondary">@color/secondary_text</item>
    </style>

    <style name="AppTheme.Purple" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/primary_purple</item>
        <item name="colorPrimaryDark">@color/primary_dark_purple</item>
        <item name="colorAccent">@color/accent_purple</item>
        <item name="colorControlHighlight">@color/primary_purple</item>
        <item name="android:textColorPrimary">@color/primary_text</item>
        <item name="android:textColorSecondary">@color/secondary_text</item>
    </style>

</resources>

v21/style.xml

<resources>

    <style name="AppTheme.Base.Green" parent="AppTheme.Green">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:navigationBarColor">@color/primary_green</item>
    </style>

    <style name="AppTheme.Base.Green.Dark" parent="AppTheme.Green">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg_black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:navigationBarColor">@color/primary_green</item>
    </style>

    <style name="AppTheme.Base.Purple" parent="AppTheme.Purple">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:navigationBarColor">@color/primary_purple</item>
    </style>

    <style name="AppTheme.Base.Purple.Dark" parent="AppTheme.Purple">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@color/activity_bg_black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:navigationBarColor">@color/primary_purple</item>
    </style>

    <!-- Base application themes. -->
    <style name="ThemeApp.Green" parent="AppTheme.Base.Green"/>
    <style name="ThemeApp.Green.Dark" parent="AppTheme.Base.Green.Dark"/>
    <style name="ThemeApp.Purple" parent="AppTheme.Base.Purple"/>
    <style name="ThemeApp.Purple.Dark" parent="AppTheme.Base.Purple.Dark"/>


    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>


    <!--   <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

       <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />-->

</resources>

method for change Theme runtime

private void setAppTheme() {

        if (!MainController.preferenceGetString(Theme_Current, "").equals("")) {
            if (MainController.preferenceGetString(Theme_Current, "").equals("Green")) {
                setTheme(R.style.ThemeApp_Green);
            } else if (MainController.preferenceGetString(Theme_Current, "").equals("Green_Dark")) {
                setTheme(R.style.ThemeApp_Green_Dark);
            } else if (MainController.preferenceGetString(Theme_Current, "").equals("Purple_Dark")) {
                setTheme(R.style.ThemeApp_Purple_Dark);
            } else if (MainController.preferenceGetString(Theme_Current, "").equals("Purple")) {
                setTheme(R.style.ThemeApp_Purple);
            }
        } else {
            setTheme(R.style.ThemeApp_Green);
        }
    }

after execute this method restart your activity needed

full source code https://github.com/rkoshti/DynamicMaterialTheme

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement