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.
Related
I am currently getting started with Material components themes. The Getting Started guide mentions these themes:
Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight
Theme.MaterialComponents.DayNight.NoActionBar
Theme.MaterialComponents.DayNight.DarkActionBar
There is no description for them though. Where can I find more details about those themes or could anyone be so kind to explain them in an answer? Just for example:
What means [..].NoActionBar? If I don't want an action bar, I don't include it in my layout file, so I really don't get this.
When would I use only Theme.MaterialComponents? Or would I always use Theme.MaterialComponents.Light or Theme.MaterialComponents.DayNight?
How do these themes look like? I couldn't find any preview and don't want to try them all out by myself - could take some time with all the widgets available on Android.
What do these themes define? Only colors? Text style? Font family?
I'm certain that I won't manage to answer all of your questions exhaustively but I'll share some thoughts, maybe it will take you one or two steps further:
What means [..].NoActionBar?
You should use a theme ending with ".NoActionBar" if you don't want the runtime to add an ActionBar to the Activity, possibly because you use a Toolbar. If you choose for example Theme.MaterialComponents.Light and use a Toolbar as well, you will notice that now your app will be renderd with two ActionBars
Please note that if some theme is called "MyTheme" then a theme which is prefixed by "MyTheme." will inherit everything from "MyTheme". So Theme.MaterialComponents.Light.NoActionBar is almost the same as Theme.MaterialComponents.Light except for the ActionBar.
So if you really don't want any ActionBar you should choose the .NoActionBar version and not include a surrogate (Toolbar) in your layout files.
What do these themes define? Only colors? Text style? Font family?
They do that, but they define also sizes, margins and behavior. In addition to that, some of them have special style combos for certain widgets (like style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox" for TextInputLayout)
For a deep dive into what properties can be influenced by a theme, you can take a look at the source of themes_material.xml
When would I use only Theme.MaterialComponents? Or would I always use Theme.MaterialComponents.Light or Theme.MaterialComponents.DayNight?
Since Theme.MaterialComponents is the parent theme of Theme.MaterialComponents.Light, they have much in common. The main difference is that the first assumes that your app will have a dark background (so the text should be white) whereas the second will have black text and assumes your app's background will be light.
Generally, one will try to use a certain theme as a template which then can be/ has to be customized - for example with a special accent color.
How do these themes look like?
You can get a general idea by switching from one to the other in a small sample app of your own.
It's true that there is not one single source of information (aka the android theming bible). You have already found material.io, but maybe the Styles and Themes section from developer.android.com or this blog post on DayNight — Adding a dark theme to your app will shed some more light.
I read in multiple places that themes are immutable. However, from this [method](https://developer.android.com/reference/android/content/res/Resources.Theme.html#applyStyle(int, boolean)) and this answer, I don't see how that is true. What do people mean when they say themes are immutable?
By immutable they mean that if you define a theme in a styles file, in XML, you cannot change its properties dynamically (programatically).
See the answer provided here: How to programmatically create an Android theme style?
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.
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.
Does anyone know how to apply the Light theme to single controls? While using the standard Holo-Dark-theme, I want to do something like this:
<CheckBox style="#android:style/Widget.Holo.CompoundButton.CheckBox"></CheckBox>
But for some reason that doesn't work.
You can not apply a theme to a single control. Themes can only be applied to activites. You can apply a style to single control but this does not help in this case as you already noticed because the Widget.Holo.CompoundButton.CheckBox style does not add anything to the parent style Widget.CompoundButton.CheckBox.
Instead the whole styling takes place in the listChoiceIndicatorSingle selector of the current theme.
So your only choice is to replace this selector by your own as described e.g. here: Setting Theme.Holo.Light changes everything except checkboxes in list
Read up on this page about Styles and Themes in Android. It goes over how to define which theme to use for a specific Activity or a whole Application. You will also see how to customize themes and styles, and how to mix and match them.