How can I change both text and background colors when my button is pressed, with xml ?
To change text color I can do :
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="mycolor"/> <item android:color="mycolor2"/> </selector>
To change the background I can do (using it in a selector/item with drawable reference) :
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF0079FF" /> </shape>
But how can I do both ? Let's say I want to have :
I totaly forgot that the background and text color are managed separately, so this is how I did it :
<Button android:textColor="@color/filtersbuttoncolors" android:background="@drawable/mybackgroundcolors" />
In mybackgroundcolors.xml I manage the background and in filtersbuttoncolors.xml I manage the text color. In both xml files I manage the status (pressed, selected, default)
To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.
Use HTML DOM Style backgroundColor Property to change the background color after clicking the button. This property is used to set the background-color of an element.
Use a semi-colon to separate the different style elements in the HTML button tag. Type color: in the quotation marks after "style=". This element is used to change the text color in the button. You can place style elements in any order in the quotation markers after "style=".
To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.
Since API level 21 you can use :
android:backgroundTint="@android:color/white"
you only have to add this in your xml
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