Change theme color at run time on Android

The theme color can be changed if several themes are added to the application and can be easily switched at runtime. Another way is to change the color of actiobar, textview, etc. One by one from the whole facility. This path is very long and requires a lot of code. The easy way I found is to change the declare color in the colors.xml file, but I could not find a way programmatically or a third-party library for this. Is there any way to do this.

My Colors XML File:

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#ea1e63</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> </resources> 

I want to change color colorPrimary at runtime, this will change color in all application actions. I found https://github.com/negusoft/GreenMatter to change colors at runtime, but its old and don't work with android studio.

+6
source share
1 answer

To change the color of a theme at runtime,

UPDATE

when you apply a theme and want to change color than

EX:

 activity.setTheme(R.style.green); <style name="green"> <item name="main_background">@drawable/background_green</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="button_light">@color/button_light</item> </style> 

The theme color cannot be changed at runtime for a particular theme .

Create your own style and change these styles when you want to change the color in the application.

+2
source

All Articles