Changing style programmatically ANDROID - android

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?

Related

How can I change app colors programmatically?

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. :-)

Is there any way to create app theme manually without pre-defined attributes?

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.

Android Change Color Resource Value Programmatically

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.

Override a style in Android without modifying layout file

My experience up until now when dealing with styles has been to create a style.xml file and create the properties I want for the style. If I want my style to be based on an existing style, I use the parent attribute. I then specify the style inside of my layout file on the controls that I want to apply the style to.
Where I am at a loss is when I want to use system styles and only update certain properties. I am wondering whether I can leave the layout files alone and not bother applying any styles to the controls. Instead, I would somehow update the property of the system style and that would update everywhere in my app where that style is already being used by default.
More specifically, I want to change the background color of the Actionbar but haven't found a way of doing it other than the way I described above.
You're probably looking for themes, which are collections of styles, applied either globally throughout the application, or for each Activity in particular. Start with this document and investigate further.

Read style items programmatically

A style defined in XML resources cannot be "applied" at runtime.
So, given a style name, I would like to read the style items one by one, interpret them and apply them programmatically to selected widgets.
Because sometimes, a user may want to change the style of some widgets at runtime an I want that style to be exactly as the one defined in the resources.
The only reason I want to do it this way is to preserve consistency between the style defined in the XML resource and the style I appply at runtime.
How can I read the style items? I know one way: to parse the XML file myself, but maybe there are some built in methods to directly read the style I am not aware of.

Categories

Resources