06 November 2016

Android Dynamically Change Style colors

In this Tutorial I will discuss How to Change Android Style theme colors. Last few days I had to I have searched how to change android theme style color dynamically but android doesn't support to change theme colors dynamically. (Note :You can changed android pre-defined theme styles dynamically not the dynamic generated themes)

If we using Theme.AppCompat.Light.DarkActionBar then these are the main properties in layout.



If You want to change theme what you did was change the primary color and primaryDark color. And this code helps you to dynamically change the theme (above mention colors) colors.

This is for change primary Dark color.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static void setStatusBarColor(String color) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = currentActivity.getWindow();
        int statusBarColor = Color.parseColor(color);

        if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }
        window.setStatusBarColor(statusBarColor);
    }
}




This is for change primary color


1
2
getSupportActionBar().setBackgroundDrawable(
        new ColorDrawable(Color.parseColor("#AA3939")));



I hope this article help you to resolve your problem.
Thanks

No comments:

Post a Comment