I am using a selector
to change the background drawable of the button in different states (focused, pressed, normal). Is there any way to change the text color too? I want to provide various text colors for various button states, but I want to do it from the xml. Is this possible?
To set Android Button text color, we can assign android:textColor XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button text color, we can pass specified color to the method Button. setTextColor(new_color).
Change Android Button Background Color Programmatically In this example project, we will change the Button background color programmatically in MainActivity. kt program using Button. setBackgroundColor(color).
To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.
Yes it can be done. You just do the same way you do for the button drawable. Then assign it to android:textColor="@drawable/yourselector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
Try combining the above with the android:drawable
attribute.
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