Is there a way to apply a style to a button when the button is pressed?
If I have a style in style.xml:
<resources>
<style name="test">
<item name="android:textStyle">bold</item>
</style>
</resources>
a selector in button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/test_pressed"
style="@style/test"
android:state_pressed="true"/>
<item android:drawable="@drawable/test_focused"
android:state_focused="true"/>
<item android:drawable="@drawable/test_normal"/>
</selector>
then how would I reference button.xml in my layout?
<Button
...
android:???="button"/>
Thanks!
You can achieve this by using an XML button definition.
Create an XML file in your drawable folder as follows:
As you can see this allows you to define different button images to use for the different states (black_button, green_button etc should be .PNG files in your drawable folder also)
Now, from your layout.xml file you can just set the button background to point to the button selector:
The Selector XML can then be referenced from teh drawable folder like any image file can.
To Change Android Button on Click/Press Color :
Define Color Value
To define color value, you have to create colors.xml file in your project values directory and add following. res/values/colors.xml
Create a XML File in Drawable Directory
Create a button_background.xml file in your drawable folder and add button pressed/clicked, focused and default color. Final code of button_background.xml file looks like this. res/drawable/button_background.xml
Adding Buttons
res/activity_main.xml
Source here.
You can make use of Color State List Resource
Example from link:
XML file saved at res/color/button_text.xml:
This layout XML will apply the color list to a View:
Romain Guy suggests it is not possible:
http://code.google.com/p/android/issues/detail?id=8941
To refer to the selector, you must give it as the background for that Button.
And for Bold when pressed, I haven't tried it either, but maybe you can mention text style in your selector, i.s.o referring to a style from it:
If this also does not work, you can do it in button's onClick().
I've not tried it but presumably you could just include
android:id="button"
in your selector.