Change android button background color

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:

Источник

how to change background image of button when clicked/focused?

I want to change the background image of a button when clicked or focused.

This is my code:

Is this code right? Does it calls a button on its event?

8 Answers 8

you can implement in a xml file for this as follows:

now save this xml file in drawable folder and name it suppos abc.xml and set it as follows

Hope it will help you. 🙂

Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button’s background in your layout file.

button_background_selector.xml

Now set the above file in button’s background.

Sorry this is wrong.

For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.

For example: button_selector.xml (define this file inside the drawable folder)

And apply it as:

use this code create xml file in drawable folder name:button

and in button xml file

To change the button background we can follow 2 methods

In the button OnClick, just add this code:

2.Create button_background.xml in the drawable folder.(using xml)

res -> drawable -> button_background.xml

Now set the above file in button’s background file.

2nd method is better for setting the background fd button

Читайте также:  What class microsd android

You just need to set background and give previous.xml file in background of button in your layout file.

and done.Edit Following is previous.xml file in drawable directory

You can also create shapes directly inside the item tag, in case you want to add some more details to your view, like this:

Beware that Android will cycle through the items from top to bottom, therefore, you must place the item without condition on the bottom of the list (so it acts like a default/fallback).

Источник

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:

Читайте также:  Framelayout android для чего используется

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.

Источник

android set button background programmatically

I would like to know how to set the button color programatically? I have coded the following but fails:

10 Answers 10

R.color.red is an ID (which is also an int ), but is not a color.

Use one of the following instead:

Or, for more pro skills:

Old thread, but learned something new, hope this might help someone.

If you want to change the background color but retain other styles, then below might help.

The answer you’re looking for in 2020 and beyond:

  • setColorFilter(color, mode) is deprecated since API 29 (as discussed here)
  • button.setBackgroundColor(color) messes with the button style

Now the proper way to set a buttons color is using BlendModeColorFilter() (see documentation).

Usage:

If you work with older APIs too:

Please vote to help others finding this answer — it took me quite a while figuring this out ^^

You can set your desired color to the button programmatically like:

Also you can give the text color for a button like:

I have found that Android Studio gives me a warning that getColor() is deprecated when trying to do this:

So I found doing the method below to be the simple, up-to-date solution:

You want to avoid hard-coding in the color argument, as it is considered bad code style.

Edit: After using setBackgroundColor() with my own button, I saw that the internal button padding expanded. I couldn’t find any way of changing it back to having both height and width set to «wrap_content». Maybe its a bug.

Источник

Оцените статью