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?
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.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With