I want change to my primary colors at runtime. I can change it with custom themes and colors. But I want to change my color from service result not static styles code. Is there any way to change colors.xml programmatically.
I find a solution but that's deprecated now: https://stackoverflow.com/a/34178187/6155031
Create one Singleton class that defines all the colors that you want
to change in your app.
Set its properties on app load from JSON file
obtained from the cloud.
In your app, wherever you are using color codes, don't set those values in xml. Set color values in your Java/Kotlin code.
I assume you are aware of findViewbyId. :-)
Related
Assume that, If we not pre-defined a theme, some style, some color in XML resources files. Do we able to create our theme at runtime programmatically and use that theme afterward?
I mean:
If we just defined a default app theme, Can we change the value of theme attrs to be a value that not defined in resource xml files programmatically? (like set the colorPrimary to be #7e57c2 in some class but effect whole app) or change the reference of it to another color source like local storage or some external files.
If we store values(colorPrimary: "#7e57c2") in local storage or sharedPref and we use a custom view instead of the default. Can we get that color and use it in our custom view class?
I wondering that If I didn't define several themes in resource files but just one default theme and then I fetched the theme's values from the backend. What is the proper way to do to use that value instead of some defined value?
P.S. I'm not intending to switch between pre-defined style at runtime. I want to know how to create a new style or edit the default value and use it afterward at runtime.
Thank you.
I am making an app and was wondering if there was a way to change a color resource value programmatically. For example, I am using the resource R.color.text_color_name to set the TextView text color. Is there a way to set that value to something different so that it would change the color of every TextView in the application?
The R file contains constants, you cant change them at runtime because you can't normally change constants at run time. If you want to update the color of all textviews why dont you look into themes. Create a custom theme and then change the theme when activity loads in onCreate. After you set the new theme i think your going to have to call setContentView again and then call all your findViewByIds again as they will be null. You could also try Calling recreate() after setTheme(). This sounds messy.
Maybe this can help you change the theme.
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.
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
I was not able to locate a simple way to resolve this issue either. By creating a ThemeColor class that holds all the colors returned from the API. Next for each activity I needed to address each widget separately as well as style it.
I am building an app that uses simple text and I would like to add an option to change the text color.
My question is would the most efficient way to do this be to assign all text views to a single STYLE resource and change the values of the STYLE resource ( if this is even possible)
or
would it be better to make multiple style resources and some how programmatically change every single View's style?
Is there anyway to inherit style from a parent or put all of the Views into a single object so its easier to just change the style of that one object?
How would you implement different color themes in your app?
All I can see now is plain set color onCreate every activity and control...
Also, how would you store different color schemes in xml?
Just an entries of with different names?
Thank you!
Use custom Themes, which are declared in XML. They are very similar to CSS, if you've used them before.
EDIT:
Here's a better example of changing the theme at run time