Android Dynamically Change Style colors

Sajith vijesekara
2 min readNov 6, 2016

--

Tips to change android style colors dynamically

android change style colors

Hi All This is my first article in medium. I am android developer and blogger in srilanka. 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.

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 in Android theme Style

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

I hope this article help you to resolve your problem.

Thanks

--

--

Sajith vijesekara
Sajith vijesekara

Written by Sajith vijesekara

Technical Lead. Passionate about cloud computing & web security | Freelance Mobile Developer| CKAD | AWS Community Builder 🇱🇰

Responses (1)