views:

110

answers:

4

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"&gt;
    <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!

A: 

I've not tried it but presumably you could just include android:id="button" in your selector.

FixerMark
I'm a little confused with by your answer. Could you elaborate on this? So I would reference button.xml in my layout using android:id="button"?
A: 
<Button
        android:background="@drawable/button"/>
Alex Volovoy
I've tried using this, but it doesn't work... have you tried it yourself? Let me know!
yes. should work just fine - see example for imageview herehttp://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Alex Volovoy
And remove style="@style/test" from your button.xml it should go to the <Button
Alex Volovoy
But if I move style="@style/test" to <Button>, wouldn't that apply @style/test to the button for all states of the button? My question was if there was a way to apply @style/test to the button when it is pressed.
A: 

To refer to the selector, you must give it as the background for that Button.

<Button
     android:background="@drawable/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:

android:textStyle="bold"

If this also does not work, you can do it in button's onClick().

mdv
android:background ignores the style in my "pressed" state in button.xml. I've tried it before. I don't think I will be easily able to apply a style to a button through XML.
A: 

You can achieve this by using an XML button definition.

Create an XML file in your drawable folder as follows:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;

        <item android:state_pressed="true"
        android:drawable="@drawable/green_button" /> 

        <!-- <item android:state_focused="true"
        android:drawable="@drawable/button_focused" /> --> 

        <item android:drawable="@drawable/black_button" />
</selector>

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:

<Button android:text="Play" android:id="@+id/playBtn"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff" />

The Selector XML can then be referenced from teh drawable folder like any image file can.

rhinds
This doesn't really address my question... this only allows me to change the background image of a button. I was asking whether I would be able to apply a style to a button (for example, bold the text) via XML when pressed.