I copy the this code from the styles.xml file in framework-res module
<style name="Theme">
<item name="colorForeground">#android:color/bright_foreground_dark</item>
<item name="colorForegroundInverse">#android:color/bright_foreground_dark_inverse</item>
<item name="colorBackground">#android:color/background_dark</item>
.
<style name="Theme.Black">
<item name="android:windowBackground">#android:color/black</item>
<item name="android:colorBackground">#android:color/black</item>
</style>
As you see, they all have a attribute name which's value is windowBackground. But the formar has a android: and the latter doesn't. Is it really necessary to write a android: prefix in android framework?
Found this to be an interesting question and tried exploring to find the answer.. This is what I found..
from: http://developer.android.com/guide/topics/resources/style-resource.html
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style
name="style_name"
parent="#[package:]style/style_to_inherit">
<item
name="[package:]style_property_name"
>style_value</item>
</style>
</resources>
item - Defines a single property for the style. Must be a child of a element.
attributes:
name
Attribute resource. Required. The name of the style property to be defined, with a package prefix if necessary (for example android:textColor).
from: http://developer.android.com/guide/topics/manifest/manifest-intro.html
Resource values
Some attributes have values that can be displayed to users — for example, a label and an icon for an activity. The values of these attributes should be localized and therefore set from a resource or theme. Resource values are expressed in the following format,
#[package:]type:name
where the package name can be omitted if the resource is in the same package as the application, type is a type of resource — such as "string" or "drawable" — and name is the name that identifies the specific resource. For example:
Values from a theme are expressed in a similar manner, but with an initial '?' rather than '#':
?[package:]type:name
And finally, I tried giving the attributes without android:, and it threw an exception, though it compiled successfully.
Accessing Platform Resources
Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the android package name. For example, Android provides a layout resource you can use for list items in a ListAdapter:
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));
In this example, simple_list_item_1 is a layout resource defined by the platform for items in a ListView. You can use this instead of creating your own layout for list items. (For more about using ListView, see the List View Tutorial.)
Related
I'm a learner of Android programming and I'm currently reading this book, well it's the first, HeadFirst Android Development.
In chapter 14 Navigation Drawers, there was this attribute of TextView textAppearance that was given a value of #style/textAppearance.AppCompat.Body1.
The book said it was a built-in styles that makes text look slightly bolder.
My question is, how many built-in styles does Android has?
I want to know all of them.
If you go through the following link you will get all the styling options for TextView.
https://developer.android.com/reference/android/widget/TextView
you can check the styling options for other views from the left panel of the same link by clicking on other classes.
A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have as the root node which is mandatory for the style file.
You can define multiple styles per file using tag but each style will have its name that uniquely identifies the style. Android style attributes are set using tag as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/>
</style>
</resources>
Using Styles
Once your style is defined, you can use it in your XML Layout file using style attribute as follows
<TextView
android:id="#+id/text_id"
style="#style/CustomFontStyle"
android:text="#string/hello_world" />
Always, when you are writing in XML to find the built-in attribute use:
attribute_name="#android:attribute_value
The #android: will list all the built-in the attribute for a given attribute you want, for Example:
style="#android:style/TextAppearance.AppCompat.Medium"
Default Styles & Themes
Android 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".
You can see the following source code for Android styles and themes- here and here
This question already has answers here:
Question mark (?) in XML attributes for Android
(2 answers)
Closed 8 years ago.
I usually set drawable in xml as android:icon="#drawable/my_icon"
But in some projects I see the code android:icon="?my_icon".
What's the difference between android:icon="#drawable/my_icon" and android:icon="?my_icon" ?
Pointing to this document and referencing the answer from there: Applying styles and themes
Just like styles, themes are also declared in XML elements, and are referenced in the same manner. The difference is that you add a theme to an entire application or activity, via the and elements in the Android Manifest — themes cannot be applied to individual Views.
Lets take an example declaration of a theme defined on the link:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme">
<item name="windowBackground">#drawable/screen_background_white</item>
<item name="panelForegroundColor">#FF000000</item>
<item name="panelBackgroundColor">#FFFFFFFF</item>
<item name="panelTextColor">?panelForegroundColor</item>
<item name="panelTextSize">14</item>
<item name="menuItemTextColor">?panelTextColor</item>
<item name="menuItemTextSize">?panelTextSize</item>
</style>
</resources>
Notice the use of the at-symbol (#) and the question-mark (?) to reference resources. The at-symbol indicates that we're referencing a resource previously defined elsewhere (which may be from this project or from the Android framework). (E.g., panelTextColor uses the same color assigned to panelForegroundColor, defined beforehand.) This technique can be used only in XML resources.
The question-mark indicates that we're referencing a resource value in the currently loaded theme. This is done by referring to a specific by its name value.
So, if you see that menuItemTextColor point to another item panelTextColor which again has question mark in front of its value. Why? Because we are again referencing a resource value from the currently loaded customTheme.
Similarly although you haven't mentioned any code but its possible that the currently loaded theme has item called my_icon whose value references a resource value pointing to some drawable in the project.
Hope this clears you a bit.
I need to associate several tags to a view so I use
view.setTag(id, tag_object)
Unfortunately Android requires to have the id as defined in a resource. However R file is auto generated of resource ids appearing in different resource files, so I do not know how to create an id detached from any resource. As work around I just use id of some resource but it isn't robust, because if I decide to remove the resource, the id can disappear. It is also reduces readability of the code having some weird id for addressing a tag. Perhaps I missed very simple trick as ids resource file.
There is a resource type "id" that lets you define arbitrary resource IDs:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="foo"/>
<item type="id" name="bar"/>
</resources>
Will generate R.id.foo and R.id.bar.
You can simply declare Strings in your strings.xml file and use those id's. For readability purpose give them some good names. And don't use these strings somewhere else in your code or resources.
I find there are two case in some demo codes, What is the difference between ?android:attr/textAppearanceMedium and ?android:textAppearanceMedium ? Thanks!
Case 1
<resources>
<style name="myTextAppearance">
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>
Case 2
<resources>
<style name="myTextAppearance">
<item name="android:textAppearance">?android:textAppearanceMedium</item>
</style>
</resources>
According to Android Developer API Guide - Accessing Resources - Referencing style attributes, the resource type is optional if the system resource tool can figure out the correct resource type. So they are referring to the same value.
I think they both do the same, that is refer to a theme attribute.
The DOCS say...
Must be a reference to another resource, in the form "#[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".
The [type:] being optional.
More explanation in the following links...
need explanation for android layout syntax
References to Theme Attributes
There are no one difference between
?android:attr/textAppearanceMedium and ?android:textAppearanceMedium.
You can use each of them, as you wish.
I tried changing the appearance of a spinner and I partly succeeded. I'm doing this via overriding parts of the theme. I managed to change the text size of the spinner item (i.e. the text size in the drop down button) with my themes.xml and styles.xml:
My themes.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="#android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:spinnerItemStyle">#style/CustomSpinnerItem</item>
</style>
</resources>
My styles.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomSpinnerItem" parent="#android:Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/CustomTextAppearance</item>
</style>
<style name="CustomTextAppearance">
<item name="android:textSize">30dp</item>
</style>
</resources>
However I cannot find the attributes that are responsible for the text appearance of the items in the dropdown list of the spinner. I tried dropDownItemStyle amongst other things. In my opinion the attribute names are not self-explanatory, so I wanted to know whether there is a documentation of what attribute does what in a style to find out which attributes to override. I found it very cumbersome to trace back all the styles used in a theme via the themes.xml and styles.xml of the platfrom and then try to find the right attributes via trial and error.
I know that one can change the appearance by passing layouts to the adapter, however, this is not really what I was looking for, since (as far as I know), you can only use inheritance in styles and not in layout xml files. If I created a custom layout for the adapter I'd have to create 9-patch images etc., which I think is a bit too time consuming in case I only want to change the text size.
Of course it's possible that I misunderstood the whole concept, since I'm new to Android ;)
You probably have found out the answer since you asked but for others looking at similar questions:
I do not know of a list of attribute names with good explanation of what they do (R.attr's page mostly gives information that is already in the name) but the way I do it is:
Start from the element I give to setDropDownViewResource(), in my case: android.R.layout.simple_spinner_dropdown_item and find.
Find its layout definition in \sdk\platforms\android-17 (specific platform version to avoid redundant results).
Get its style from the layout file. In this case: ?android:attr/spinnerDropDownItemStyle
We now have the attribute name we need.
It's better to do it that way rather than try to guess what attribute to use because you know which attribute the system itself use so it's very likely to be the correct one (unless there's a bug).
If I created a custom layout for the adapter I'd have to create
9-patch images etc.
Well, no, the layout determines what kind of GUI element you would have (a textfield, a spinner, an imagebutton, a custom element...), not how they are styled (nine-patch backgrounds, text colors...), so you still would have to mess with styles to get the right appearance.
For example, for visual consistency I ported the button, checkbox and spinner style from Theme.Holo to Gingerbread, yet I did not mess with layout, all I did was the aforementioned steps plus looking up the result (spinnerDropDownItemStyle in the above example) in themes.xml, which gave me the style name (e.g.: Widget.Holo.DropDownItem.Spinner).
Then I looked that up in styles.xml and imported it (and any parent*) in my project's styles.xml, searching and copying any Holo specific reference in my project and adjusting the namespace accordingly (add android: to attributes and replace ?android:attr with #style for what I copy to my styles.xml file).
So far I haven't had to mess with layouts at all (even the presence of radio buttons in spinner dialogs on Gingerbread is determined by an xml attribute: android:checkMark).
If a style has no parent attribute (like Widget.Holo.DropDownItem.Spinner) then its parent is the same style minus the last element (e.g.: Widget.Holo.DropDownItem)