Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change button text color when pressed

I have made my button transparent so I would like to have the button text color change when the button is pressed. Is it possible to do this using just xml files?

like image 777
skinnybrit51 Avatar asked Feb 17 '12 20:02

skinnybrit51


People also ask

How do I change the button text color in python?

We can also change the foreground color by using the 'fg' property of the button widget in Tkinter. We can also use the configure method to change the color. As we know that we can use the 'bg' and 'fg' property of the Button widget to change the color, we need to pass this variable while using the Button object.

How do you change the color of text in HTML?

To set the font color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property color. HTML5 do not support the <font> tag, so the CSS style is used to add font color.


2 Answers

Yes, you can do it like that:

layout/main_layout.xml:

.....
    <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="bonjour !"
      android:textColor="@color/button_text_color"
    />
.....

color/button_text_color.xml:

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="#c0c0c0" android:state_pressed="true"/>
     <item android:color="#ffffff"/>
   </selector>
like image 129
damson Avatar answered Oct 20 '22 12:10

damson


See the section called State List in this bit of documentation...Drawable Resources.

You can define two different Button xml files one for the transparent 'default' state and another with the button as Red for your 'pressed' state. You then define a selector which switches the drawable resources in the different states.

EDIT: As per devunwired's comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.

like image 38
Squonk Avatar answered Oct 20 '22 13:10

Squonk