Apply Different Style to Button When Pressed
Asked Answered
C

7

9

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!

Comedown answered 16/9, 2010 at 19:48 Comment(0)
E
7

Romain Guy suggests it is not possible:

http://code.google.com/p/android/issues/detail?id=8941

"Selector works only for drawables, not text appearances. There is currently not plan to make this happen."

Eames answered 9/10, 2011 at 20:36 Comment(0)
P
5

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">

        <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.

Pice answered 24/9, 2010 at 10:2 Comment(1)
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.Comedown
E
2

You can make use of Color State List Resource

Example from link:

XML file saved at res/color/button_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

This layout XML will apply the color list to a View:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />
Eroto answered 21/9, 2016 at 13:55 Comment(0)
K
2

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="button_pressed">#ff8a00</color>
    <color name="button_focused">#ff8a00</color>
    <color name="button_default">#1c76bb</color>
</resources>

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

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@color/button_pressed"/> <!-- pressed -->
    <item android:state_focused="true"
        android:drawable="@color/button_focused"/> <!-- focused -->
    <item android:drawable="@color/button_default"/> <!-- default -->
</selector>

Adding Buttons

res/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"
        android:text="Click Me"
        android:textColor="#fff" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="16dp"
        android:background="@drawable/button_background"
        android:text="Click Me"
        android:textColor="#fff" />

</RelativeLayout>

Source here.

Katydid answered 12/4, 2018 at 8:47 Comment(0)
S
0
<Button
        android:background="@drawable/button"/>
Stare answered 16/9, 2010 at 21:59 Comment(4)
I've tried using this, but it doesn't work... have you tried it yourself? Let me know!Comedown
yes. should work just fine - see example for imageview here developer.android.com/guide/topics/resources/…Stare
And remove style="@style/test" from your button.xml it should go to the <ButtonStare
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.Comedown
C
0

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().

Codeclination answered 24/9, 2010 at 9:48 Comment(1)
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.Comedown
A
-3

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

Abruzzi answered 16/9, 2010 at 19:55 Comment(1)
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"?Comedown

© 2022 - 2024 — McMap. All rights reserved.