Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Android Theme Programmatically

A similar question to this has been asked many times; however, an answer has not been given that addresses my situation. I need to dynamically change an application's theme based on color values that are being returned from an API call. I then need to change the theme colors of the app based on the values returned. Therefore, I have no way of saving the colors in a style XML file. Can this be done?

I have a base activity, and my plan is to set the app theme from there for all the activities.

like image 256
mattfred Avatar asked Mar 24 '26 03:03

mattfred


1 Answers

Unfortunately, I did not find an easy way to do this. I created a ThemeColor class which holds all the colors returned by the API. Then for each activity I have to go through every widget and style it.

Example:

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(themeColor.getActionBarColor)));
this.getWindow().getDecorView().setBackgroundColor(Color.parseColor(themeColor.getBackgroundColor()));

etc

like image 163
mattfred Avatar answered Mar 25 '26 17:03

mattfred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!