Read style items programmatically - android

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.

Related

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.

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.

Changing style programmatically 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?

What is the difference between R.style.x, and R.attr.x?

What is the difference between let's say android.R.style.TextAppearance_large and android.R.attr.textAppearanceLarge?
The attr part is just a thing which can been styled e.g. with a theme while that style is already one defined style.
So you can refer a style without knowing the style attributes. That is really helpful if you want to define a control which can been styled in multiple ways. Like for a button you have a holo style or some other device typical styles.
If you know C you can compair it with a prototype (or header file) while the style is the implementation.

What can be edited on the fly?

I understand that to apply a style you have to create the textview and apply the style programmatically, which I haven't figured out how to do yet, but what can you edit on the fly. Like text color? Background color? What things can or what cannot be edited on the fly from the java. Also can it be defined in the xml and then edited in the Java or must anything you want to change or set be defined solely from the Java?
The documentation for TextView lists the XML attributes that it supports, along with the method names for those that are supported at run time. Just about everything can be changed at run time, whether or not defined in XML (including every attribute you mentioned). The run-time changes override whatever is defined in XML.

Categories

Resources