Android: change button text and background color

How to change the color of text and background when a button is clicked using xml?

To change the color of the text, 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 / drawable element):

 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF0079FF" /> </shape> 

But how can I do both? Say I want:

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

EDIT: answer

I completely forgot that the background and text color are controlled separately, so here is how I did it:

 <Button android:textColor="@color/filtersbuttoncolors" android:background="@drawable/mybackgroundcolors" /> 

In mybackgroundcolors.xml I control the background and in filtersbuttoncolors.xml I control the color of the text. In both xml files, I control the status (clicked, selected, by default)

+49
android colors button background status
Oct. 16 '13 at 10:13
source share
5 answers

Below is an example of a drawable that will be white by default, black when clicked:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="#1E669B"/> <stroke android:width="2dp" android:color="#1B5E91"/> <corners android:radius="6dp"/> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/> </shape> </item> <item> <shape> <gradient android:angle="270" android:endColor="#1E669B" android:startColor="#1E669B"/> <stroke android:width="4dp" android:color="#1B5E91"/> <corners android:radius="7dp"/> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/> </shape> </item> </selector> 
+36
Oct. 16 '13 at 10:15
source share

I think making it a lot easier:

 button.setBackgroundColor(Color.BLACK); 

And you need import android.graphics.Color; not: import android.R.color;

Or you can simply write a 4-byte hex code (not a 3-byte) 0xFF000000 , where the first byte sets the alpha.

+16
Jun 23 '14 at 4:25
source share

Starting at API level 21, you can use:

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

you need to add this to your xml

+12
Jul 15 '16 at 16:24
source share

add line below in styles.xml

 <style name="AppTheme.Gray" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorButtonNormal">@color/colorGray</item> </style> 

in the button, add android:theme="@style/AppTheme.Gray" , example:

 <Button android:theme="@style/AppTheme.Gray" android:textColor="@color/colorWhite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@android:string/cancel"/> 
+4
Nov 30 '16 at 6:29
source share

When you create the application, a file called styles.xml will be created in the res / values ​​folder. If you change the styles, you can change the background, text color, etc. For all your layouts. Thus, you do not need to go into each individual layout and change it manually.

styles.xml:

 <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="Theme.AppBaseTheme" parent="@android:style/Theme.Light"> <item name="android:editTextColor">#295055</item> <item name="android:textColorPrimary">#295055</item> <item name="android:textColorSecondary">#295055</item> <item name="android:textColorTertiary">#295055</item> <item name="android:textColorPrimaryInverse">#295055</item> <item name="android:textColorSecondaryInverse">#295055</item> <item name="android:textColorTertiaryInverse">#295055</item> <item name="android:windowBackground">@drawable/custom_background</item> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> 

parent="@android:style/Theme.Light" - Google’s native color. Here is a link to what the native styles are: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

name="Theme.AppBaseTheme" means that you create a style that inherits all styles from parent="@android:style/Theme.Light" . This part that you can ignore if you want to inherit from AppBaseTheme again. = <style name="AppTheme" parent="AppBaseTheme">

@ drawable / custom_background is a custom image that I put in the drawables folder. This image is 300 × 300 pixels in size.

# 295055 - dark blue color.

My code changes the background color and text. For the button text, please browse the Googles native styles (the link I gave above).

Then in Android Manifest do not forget to specify the code:

 <application android:theme="@style/Theme.AppBaseTheme"> 
+3
Feb 24 '14 at 9:46
source share



All Articles