Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : change button text and background color

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 :

  • Default : black text / white background
  • Pressed : white text / blue background

EDIT : answer

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)

like image 547
Maelig Avatar asked Oct 16 '13 10:10

Maelig


People also ask

How can change background color of button in android?

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.

How do I change the background color of my clicking 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.

How do I change the color of my text buttons?

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

How do I change the default button 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.


1 Answers

Since API level 21 you can use :

android:backgroundTint="@android:color/white" 

you only have to add this in your xml

like image 134
Jonsmoke Avatar answered Oct 13 '22 00:10

Jonsmoke