- How to Change the Background Color of Button in Android using ColorStateList?
- Approach
- Android : change button text and background color
- EDIT : answer
- 7 Answers 7
- Android changing Floating Action Button color
- 25 Answers 25
- Can’t change background color on MaterialButton without change colorAccent
- 10 Answers 10
- Standard Android Button with a different color
- 20 Answers 20
- Sample Button widget
How to Change the Background Color of Button in Android using ColorStateList?
ColorStateList is an object which can define in an XML file that can be used to apply different colors on widgets (such as Buttons, etc) depending on the state of Widgets to which it is being applied. For Example, There are many states of Buttons like (pressed, focussed, or none of them ) and other widgets states like enable, checkable, checked, etc, Using Color State List is a nice way to change the color of the button without using shape drawables or custom images. One should remember the color state list can be used anywhere, where color is used. The color state list is defined in XML and saved under the res/color folder. The root element of the color state list is a selector and the item element is defined for each state that you want to define the color by using color and alpha attributes. The default color should be the last element which is used when color for a specific state is not defined. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
Approach
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Источник
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 :
To change the background I can do (using it in a selector/item with drawable reference) :
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 :
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)
7 Answers 7
Since API level 21 you can use :
you only have to add this in your xml
Here is an example of a drawable that will be white by default, black when pressed:
I think doing this way is much simpler:
And you need to import android.graphics.Color; not: import android.R.color;
Or you can just write the 4-byte hex code (not 3-byte) 0xFF000000 where the first byte is setting the alpha.
Just complementing @Jonsmoke’s answer.
For API level 21 and above you can use :
in XML for the button layout.
For API level below 21 use an AppCompatButton using app namespace instead of android for backgroundTint.
add below line in styles.xml
in button, add android:theme=»@style/AppTheme.Gray» , example:
When you create an App, a file called styles.xml will be created in your res/values folder. If you change the styles, you can change the background, text color, etc for all your layouts. That way you don’t have to go into each individual layout and change the it manually.
name=»Theme.AppBaseTheme» means that you are creating a style that inherits all the styles from parent=»@android:style/Theme.Light» . This part you can ignore unless you want to inherit from AppBaseTheme again. =
@drawable/custom_background is a custom image I put in the drawable’s folder. It is a 300×300 png image.
#295055 is a dark blue color.
My code changes the background and text color. For Button text, please look through Google’s native stlyes (the link I gave u above).
Then in Android Manifest, remember to include the code:
Источник
Android changing Floating Action Button color
I have been trying to change Material’s Floating Action Button color, but without success.
I have tried to add:
But none of the above worked. I have also tried the solutions in the proposed duplicate question, but none of them works; the button remained green and also became a square.
P.S. It would be also nice to know how to add ripple effect, couldn’t understand that either.
25 Answers 25
As described in the documentation, by default it takes the color set in styles.xml attribute colorAccent.
The background color of this view defaults to the your theme’s colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).
If you wish to change the color
- in XML with attribute app:backgroundTint
- in code with .setBackgroundTintList (answer below by ywwynm)
As @Dantalian mentioned in the comments, if you wish to change the icon color for Design Support Library up to v22 (inclusive), you can use
For Design Support Library since v23 for you can use:
Also with androidX libraries you need to set a 0dp border in your xml layout:
Vijet Badigannavar’s answer is correct but using ColorStateList is usually complicated and he didn’t tell us how to do it. Since we often focus on changing View ‘s color in normal and pressed state, I’m going to add more details:
If you want to change FAB ‘s color in normal state, you can just write
If you want to change FAB ‘s color in pressed state, thanks for Design Support Library 22.2.1, you can just write
By setting this attribute, when you long-pressed the FAB , a ripple with your color will appear at your touch point and reveal into whole surface of FAB . Please notice that it won’t change FAB ‘s color in normal state. Below API 21(Lollipop), there is no ripple effect but FAB ‘s color will still change when you’re pressing it.
Finally, if you want to implement more complex effect for states, then you should dig deeply into ColorStateList , here is a SO question discussing it: How do I create ColorStateList programmatically?.
UPDATE: Thanks for @Kaitlyn’s comment. To remove stroke of FAB using backgroundTint as its color, you can set app:borderWidth=»0dp» in your xml.
Источник
Can’t change background color on MaterialButton without change colorAccent
Android Studio 3.2.1 Here my layout:
to change MaterialButton’s background I change colorAccent in styles.xml
But the problem is: I do not want to change colorAccent. I want to use background’s color for MaterialButton’s different from colorAccent
10 Answers 10
1st Solution
You can use app:backgroundTint to change back ground color of MaterialButton
2nd Solution
MaterialButton uses colorPrimary as background when button is in active state and colorOnSurface when disabled. So, you can define it in your theme and apply it on material buttons
With the MaterialButton you have 2 options:
Using the backgroundTint attribute as suggest by Zaid Mirza
If you want to override some theme attributes from a default style you can use new materialThemeOverlay attribute. It is the best option in my opinion.
It requires at least the version 1.1.0 of the library.
If you want to set custom drawable you need to make the app:backgroundTint=»@null» . For just changing the background colour app:backgroundTint=»@color/yourColor»
I’m currently using 1.3.0-alpha01
2020: It seems that they just fixed this on April 1st 2020.
It should be released on 1.2.0 beta 1 since the GitHub issue was closed as «Fixed»
You can do it by following the below code.
backgroundTint also changed the disabled state color so wasn’t good for me
Best solution i could find was to override the primary color for the MaterialButton (only) through overlay style
Add this code to your styles.
Replace ?attr/colorSecondary to whatever color you want
Add the theme to the button
If you you using MDC and you want to change the theme for all buttons:
Add this row to your themes.xml
and add these lines to your type.xml
In that case you don’t need to add android:theme=»@style/MyButtonTheme» to your MaterialButton
If any mistake please let me know and don’t be hurry to downgrade
BackgroundTint Always works on material buttons, but first, do uninstall the app and reinstall it again. Sometimes changes may not reflect until you reinstall the app.
android:backgroundTint is applied over the android:background and their combination can be controlled by android:backgroundTintMode
do check this answer for difference between android:background , android:backgroundTint and android:backgroundTintMode
Change the backgroundTintMode to add and then your background attribute will be displayed. See example below:
Источник
Standard Android Button with a different color
I’d like to change the color of a standard Android button slightly in order to better match a client’s branding.
The best way I’ve found to do this so far is to change the Button ‘s drawable to the drawable located in res/drawable/red_button.xml :
But doing that requires that I actually create three different drawables for each button I want to customize (one for the button at rest, one when focused, and one when pressed). That seems more complicated and non-DRY than I need.
All I really want to do is apply some sort of color transform to the button. Is there an easier way to go about changing a button’s color than I’m doing?
20 Answers 20
I discovered that this can all be done in one file fairly easily. Put something like the following code in a file named custom_button.xml and then set background=»@drawable/custom_button» in your button view:
Following on from Tomasz’s answer, you can also programmatically set the shade of the entire button using the PorterDuff multiply mode. This will change the button colour rather than just the tint.
If you start with a standard grey shaded button:
will give you a red shaded button,
will give you a green shaded button etc., where the first value is the colour in hex format.
It works by multiplying the current button colour value by your colour value. I’m sure there’s also a lot more you can do with these modes.
Mike, you might be interested in color filters.
try this to achieve the color you want.
This is my solution which perfectly works starting from API 15. This solution keeps all default button click effects, like material RippleEffect . I have not tested it on lower APIs, but it should work.
All you need to do, is:
1) Create a style which changes only colorAccent :
I recommend using ThemeOverlay.AppCompat or your main AppTheme as parent, to keep the rest of your styles.
2) Add these two lines to your button widget:
Sometimes your new colorAccent isn’t showing in Android Studio Preview, but when you launch your app on the phone, the color will be changed.
Sample Button widget
You can now also use appcompat-v7’s AppCompatButton with the backgroundTint attribute:
I like the color filter suggestion in previous answers from @conjugatedirection and @Tomasz; However, I found that the code provided so far wasn’t as easily applied as I expected.
First, it wasn’t mentioned where to apply and clear the color filter. It’s possible that there are other good places to do this, but what came to mind for me was an OnTouchListener.
From my reading of the original question, the ideal solution would be one that does not involve any images. The accepted answer using custom_button.xml from @emmby is probably a better fit than color filters if that’s your goal. In my case, I’m starting with a png image from a UI designer of what the button is supposed to look like. If I set the button background to this image, the default highlight feedback is lost completely. This code replaces that behavior with a programmatic darkening effect.
I extracted this as a separate class for application to multiple buttons — shown as anonymous inner class just to get the idea.
Источник