- Android : change button text and background color
- EDIT : answer
- 7 Answers 7
- Change colour of button in Android Studio
- Method 1: Using XML
- Method 2: Using a drawable file
- Android changing Floating Action Button color
- 25 Answers 25
- 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?
- Approach
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:
Источник
Change colour of button in Android Studio
As you know, the Default color of the button in the android studio is grey which is not suits for UI. So, here we are going to discuss how to change the colour of the button in android studio using different methods
Table of Contents
Method 1: Using XML
First, add the button in XML layout like this
Normal color of button
After this you will see your button looks something like this
Now you need to add another tag that is android:background=”color Hexa code” like this,
Now, you see your button looks like this
coloured color of button
Method 2: Using a drawable file
This is my best method because it provides you many other operations to perform on the button like rounded corner etc.
Right click on drawable -> new-> drawable resource file and named as background.
Add following line of code there
You see something like this
drawable file
Now, goto your button and drawable like this
coloured button
We update this content as we see more and more methods to change the colour of button in android studio.
How to add button in android studio?
Simply add from the design section of XML layout.
How to change the colour of button in android studio?
Add android:background=”colour” in button
Источник
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.
Источник
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.
Источник
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.
Источник