Finding all available styles defined by android platform themes - android

In several android styling tutorials parent style elements are used that i can not find in the android.R.style.* styles (http://developer.android.com/reference/android/R.style.html).
A few examples are from the http://android-developers.blogspot.com/2011/04/customizing-action-bar.html action bar article. Nick uses parent styles like:
<style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner.DropDown.ActionBar">
...
</style>
or
<style name="MyActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBarView_TabView">
...
</style>
Where do these parent styles come from? Is it possible to list all available parent styles?
Thanks.

As stated in the "Applying Styles and Themes" article:
The Android platform provides a large collection of styles and themes
that you can use in your applications. You can find a reference of all
available styles in the R.style class. To use the styles listed here,
replace all underscores in the style name with a period. For example,
you can apply the Theme_NoTitleBar theme with
"#android:style/Theme.NoTitleBar".
The R.style reference, however, is not well documented and does not
thoroughly describe the styles, so viewing the actual source code for
these styles and themes will give you a better understanding of what
style properties each one provides. For a better reference to the
Android styles and themes, see the following source code:
Android Styles (styles.xml)
Android Themes (themes.xml)
These files will help you learn through example. For instance, in the
Android themes source code, you'll find a declaration for <style
name="Theme.Dialog">. In this definition, you'll see all of the
properties that are used to style dialogs that are used by the Android
framework.

For "android:style/Widget.Holo.Light.ActionBarView_TabView" see:
http://developer.android.com/reference/android/R.style.html#Widget_Holo_Light_ActionBar_TabView
...and note the API level it was introduced at!
For "android:style/Widget.Holo.Light.Spinner.DropDown.ActionBar" try:
http://developer.android.com/reference/android/R.style.html#Widget_Holo_Light_DropDownItem_Spinner
The latter is just my best guess - I can't get the list to actually drop down.

Parent attribute is a optional one while creating your own themes or styles.
Basically parent attributes are used for inheriting the styles which are already defined in the platform itself.
If we wish to inherit a theme or style and overwriting some features, we can use parent attribute.
Here's a link for the all available platform defined styles.
And one more tip :
If you want to inherit your own style that you created by yourself means follow this example to inherit.
<style name="MyDropDownNav.Red">
<item name="android:textColor">#FF0000</item>
</style>

Here is Official Google's Documentation for Default List of Themes and Styles
Google Android Documentation
You can Find here in this documentation
Android Styles (styles.xml)
Android Themes (themes.xml)
I know the answer to above question finishes over here still giving way of implementation, as it will be helpful for novices..
How to Implement them
This is what i got from the themes.xml
<style name="Theme.NoTitleBar">
<item name="android:windowNoTitle">true</item>
</style>
Now to implement it in my project
I have to add the following line in my Styles.xml file
<style name="AppTheme" parent="android:Theme.NoTitleBar" />
In this I am creating my own theme named "AppTheme" which will inherit its properties from already defined them by google. so I am mentioning it in parent
By looking into themes.xml and styles.xml will not only help to get the list of the already defined themes and styles but also help novices to have an idea how to create the themes.
As the theme I am mentioning here in parent is already defined by android so we will use it by prefixing the android: to theme name ..
If You want to inherit your created theme then
<style name="Theme2" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
then just provide the style name to the parent, here i am using Apptheme as parent theme for theme2

Related

Why and when we need to add android: prefix to style?

I have seen that some Style attributes require android prefix and some don't need it. What is the reason. like
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Why we haven't use android:windowActionBar and android:windowNoTitle
Based on SDK version Android styles are grouped in different namespaces. In order to override a Theme you have to follow it's namespace.
That's the reason you don't need the android: prefix when extending an AppCompat Theme, but if you wanted something else, let's say Theme.Holo - you'd have 2 different styles for the them - one for pre-Lollipop devices, and one for -21, the latter having the android: prefix before each style attribute.
It depends on which Themes you're using and which context it is. These attributes are defined by different sources.
If the attribute name is prefixed with android:, it's a framework attribute and can only be used for the Android versions having it defined.
If the attribute is not prefixed at all, the attributes are defined by your own application. This includes all attribute definitions you pulled in with libraries.
In your example you're defining a theme for AppCompat which is part of the support library and thus of your application. The framework widgets won't recognize these colors directly.

what is ColorPrimaryLight and how do I use it?

I get this as one of my colors generated by the Material Design Color Platte.
But in this image:
I don't see colorPrimaryLight anywhere. What is it used for and how do I declare it? Do declare it like this in my style?:
<item name="colorPrimaryLight">#color/colorPrimaryLight</item>
Or do I declare it in a different way? And what is it used for?
By default, the statusBarColor is set to colorPrimaryDark. If you want to use colorPrimaryLight for the status bar you need to set android:statusBarColor to android:colorPrimaryLight.
https://developer.android.com/training/material/theme.html
in your resources file put:
<color name="colorPrimaryLight">#D1C4E9</color>
in your styles file put:
<item name="colorPrimaryLight">#color/colorPrimaryLight</item>
TL;DR: colorPrimaryLight is one of several colors identified as part of the Material palette that is not actually mapped to a Theme attribute in Android. The guidelines recommend using it in several areas such as a TabLayout backgrounds.
I cannot find an authoritative source about using this attribute in Android apps, but it seems that Material defines a large number of colors, as shown on this helpful page, but not all of them are mapped to Android Theme attributes. For instance, if you create a brand new single-Activity project in Android Studio and modify styles.xml as shown below:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- THIS THEME ATTRIBUTE IS NOT FOUND -->
<item name="colorPrimaryLight">#color/colorPrimary</item>
</style>
</resources>
The colorPrimaryLight attribute is not found by the compiler.
Therefore if you wanted to use it in your application you would presumably have to explicitly link to a color.xml resource. This seems to negate the benefit of the Theme system somewhat, but perhaps I am missing a trick.

Setting name attributes in material design

I have a simple question, but I really don't get it. When we set a new style under material design, we use parent styles as follow:
<style name="AppTheme.Base" parent="Base.Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
However, in some tutorials, I saw they insert the reference "android:" into the attribute, e.g.:
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
I know that the latter should be used for referencing every attribute in android, but I don't understand why in this case it is sometimes omitted and sometimes not.
It is omitted if you are using the Android V7 Support Library. In this case you use one of the Theme.AppCompat themes, instead of the Android theme. Take a look at Maintaining Compatibility.
It is depending on which compatibility they want:
using android: prefix they refer to a platform attribute, that is it will be effective only on devices where the attribute exist (e.g. API14+, API21+ etc)
using without android: prefix they refer to an Appcompat Library's attribute linked with the app project, that is it will be effective on all devices with API 7+

trying to implement toolbar in android

I am trying to get rid of my actiobars and use toolbar to update my app. I am not making my apps for 5.0 yet, so no other material goodness.
From following another post I made my themes.xml look like this:
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">#color/mycolorprimary</item>
<item name="colorPrimaryDark">#color/mycolorprimarydark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
The first issue I am having is that "Style" is throwing this error:
Element Style must be declared
Theme vs Style
So what exactly is the difference? Well they are both declared in exactly the same way (which you already know), the difference comes in how they’re used.
Themes are meant to be the global source of styling for your app. The new functionality doesn’t change that, it just allows you to tweak it per view.
Styles are meant to be applied at a view level. Internally, when you set style on a View, the LayoutInflater will read the style and apply it to the AttributeSet before any explicit attributes (this allows you to override style values on a view).
Values in an attribute set can reference values from the View’s theme.
Themes are global, styles are local.
From theme-vs-style
I would recomend a read at the above link.

ActionBarSherlock mirrod attributes -- when to use "android:" prefix?

When styling ActionBarSherlock I was wondering when I have to use the prefixed attribute, when the non-prefixed attribute, and when both. For example:
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
I found this explanation on the ActionBarSherlock website:
Mirrored Attributes
Due to limitations in Android's theming system any theme customizations must be declared in two attributes. The normal android-prefixed attributes apply the theme to the native action bar and the unprefixed attributes are for the custom implementation. Since both theming APIs are exactly the same you need only reference your customizations twice rather than having to implement them twice.
The easiest way to convey exactly what this entails is with an example. The following is the full theme from the “Styled” example mentioned above:
<style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="background">#drawable/bg_striped</item>
<item name="android:background">#drawable/bg_striped</item>
<item name="backgroundSplit">#drawable/bg_striped_split</item>
<item name="android:backgroundSplit">#drawable/bg_striped_split</item>
</style>
I thought, non-prefixed attributes only have to be used for attributes, that did not exist before API level 11. But why is there an android:background as well as a background attribute in the example? android:background exists since API level 1. Can someone please give some more details about these mirrored attributes?
From what I understand of the ActionBarSherlock documentation you quote, the android-prefixed attribute (which you would have been the only one to set if you used the "normal" ActionBar) is used when ActionBarSherlock uses the native version of ActionBar (that is, on devices running Android 3+, where it's available), and the non-prefixed version is used on older versions, when ActionBarSherlock actually has to use its own implementation of the ActionBar component.
In short, android-prefixed attributes are used by Android native features, and non-prefixed versions are used by custom components.
Anyway, it looks like you always have to set both prefixed and non-prefixed attribute when theming an ActionBarSherlock object.
Simple rule is anywhere you are inheriting from parent="Widget.Sherlock.etc" then you should have dual attributes.
The exception being direct styles like Text and Button don't as you are only passing your style to that TextView/Button directly, I would however always inherit from the parent/current style defined in the abs__styles.xml, that way you will always get the correct spacing etc..

Categories

Resources