Android - default button style
Question: Where can I find default styles xml with hexadecimal codes of colors?
I'm looking for Style 'buttonStyle' and other default styles witch affect aspects like TextViews, Buttons etc (if you dont change the style of aspect)
I looked up in <instalation_folder>\android-sdk\platforms\android-<versio>\data\res\values
and <instalation_folder>\android-sdk\platforms\android-<version>\data\res\colors
but I didn't actually find what I was looking for.
Hope my question is clear.
Due to low reputation I cant answer this question yet. Here is answer
Answer
With a bit of googling I found 'buttonStyle' is actually 'Widget.Button' - Styling Android With Defaults
This is how it works:
- As I said 'buttonStyle' style is accualy 'Widget.Button' style defined in
\android-sdk\platforms\android-<version>\data\res\values\styles.xml
. Background is set to:@android:drawable/btn_default
-
\android-sdk\platforms\android-<version>\data\res\drawable\btn_default.xml
defines background color of button as selector. Color actually depends on button's state. Default color is set to@drawable/btn_default_normal
- With a bit of searching I found, that btn_default_normal is png image located in
\android-sdk\platforms\android-<version>\data\res\drawable-mdpi
I find it a bit confusing, but I hope it will help someone, maybe...
Solution 1:
Understanding how Android styles do work can be a little bit messy.
I will try to explain how the basic work flow would be, based on an example.
Let's assume you want to know what the default background for buttons is. This can be either a simple color (unlikely) or a drawable (there are many different types of drawables).
Android has Themes. A theme basically defines which style is applied to which widget. Therefore, our first step is to find the default android theme.
You find it under android-sdk\platforms\android-15\data\res\values\themes.xml
In this theme file, search for button
.
You will find something like this:
<!-- Button styles -->
<item name="buttonStyle">@android:style/Widget.Button</item>
This means that the theme applies the style Widget.Button
to buttons.
Ok, now let's find the style Widget.Button
.
All default Android style are defined in the file android-sdk\platforms\android-15\data\res\values\styles.xml
Now search for Widget.Button
You will find something like this:
<style name="Widget.Button">
<item name="android:background">@android:drawable/btn_default</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
<item name="android:textColor">@android:color/primary_text_light</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
The interesting line is:
<item name="android:background">@android:drawable/btn_default</item>
This means that there is a drawable called btn_default
set as button background.
Now we need to find a file named btn_default.*
in one of the drawable folders under android-sdk\platforms\android-15\data\res
.
This can be either an image (very unlikely) or a xml file like btn_default.xml
.
After a little bit searching you will find the file android-sdk\platforms\android-15\data\res\drawable\btn_default.xml
It contains something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />
<item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" />
<item android:drawable="@drawable/btn_default_normal_disable" />
</selector>
Now you have to understand that this is a selector drawable (one of the many drawable types). This selector chooses different backgrounds, based on the buttons state. For example, if the buttons is pressed, it has a different background.
No let's look at the default state.
<item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />
It applies a drawable called btn_default_normal
.
Now we need to find this drawable.
Again, we need to find a file named btn_default_normal.*
in one of the drawable folders under android-sdk\platforms\android-15\data\res
.
This can be again either an image or a xml file like btn_default_normal.xml
.
You will find multiple files called 'btn_default_normal.9.png' in different drawable folders for different resolutions.
:) Now you know that btn_default_normal.9.png
is set as button background.
Solution 2:
You can find default styles of android widgets when you declare in XML:
style="@android:style/Widget.Button" - standard Button
style="@android:style/Widget.TextView" - standard TextView
For example this is style of default button:
<style name="Widget.Button">
<item name="android:background">@android:drawable/btn_default</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
<item name="android:textColor">@android:color/primary_text_light</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
background of default button:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_default_normal" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@drawable/btn_default_normal_disable" android:state_enabled="false" android:state_window_focused="false"/>
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_default_selected" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/btn_default_normal" android:state_enabled="true"/>
<item android:drawable="@drawable/btn_default_normal_disable_focused" android:state_focused="true"/>
<item android:drawable="@drawable/btn_default_normal_disable"/>
</selector>
You can get the default color with the help of any graphics editor: path to default button NinePatch ..\android-sdk\platforms\android-13\data\res\drawable-hdpi