44

How do i get the background color of a button. In the xml i set the background color using ---- android:background = XXXXX now in the activity class how can i retrieve this value that it has ?

LostPuppy
  • 2,471
  • 4
  • 23
  • 34

7 Answers7

87

Unfortunately I don't know how to retrieve the actual color.

It's easy to get this as a Drawable

Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();

If you know this is a color then you can try

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();

And if you're on Android 3.0+ you can get out the resource id of the color.

int colorId = buttonColor.getColor();

And compare this to your assigned colors, ie.

if (colorID == R.color.green) {
  log("color is green");
}
Matthew Rudy
  • 16,724
  • 3
  • 46
  • 44
  • 2
    Are you sure getColor() gets the id? I think it gets the actual int value of the color (ie. 0xAARRGGBB). I tested this with "#00000001" and it returned 1. – brianestey Nov 11 '11 at 03:41
  • 1
    In my test I did `button.setBackground(R.color.green)` and then checked the response and it definitely wasn't the actual color id. I'd prefer a proper color integer so I can `Color.red(int)`, `Color.green(int)`, `Color.blue(int)` it. But in my test on Android 3.2, this wasn't the case. I'd guess that it's inconsistent, returning a color int, or resid, depending on context. – Matthew Rudy Nov 11 '11 at 04:27
  • Hey, I am trying to do a task only if the background image of an image button is a certain drawable resource. How can I compare... I've tried if(buttonBackground.equals(@drawable/mydrawable)) but it doesn't work – Ruchir Baronia Nov 28 '15 at 21:29
  • 3
    I used this `((ColorDrawable) row.getBackground()).getColor()` as `(row.background as ColorDrawable).color` but i faced with this error `android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable` – milad salimi Aug 08 '19 at 06:00
19
private Bitmap mBitmap;
private Canvas mCanvas;
private Rect mBounds;

public void initIfNeeded() {
  if(mBitmap == null) {
    mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    mBounds = new Rect();
  }
}

public int getBackgroundColor(View view) {
  // The actual color, not the id.
  int color = Color.BLACK;

  if(view.getBackground() instanceof ColorDrawable) {
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
      initIfNeeded();

      // If the ColorDrawable makes use of its bounds in the draw method,
      // we may not be able to get the color we want. This is not the usual
      // case before Ice Cream Sandwich (4.0.1 r1).
      // Yet, we change the bounds temporarily, just to be sure that we are
      // successful.
      ColorDrawable colorDrawable = (ColorDrawable)view.getBackground();

      mBounds.set(colorDrawable.getBounds()); // Save the original bounds.
      colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.

      colorDrawable.draw(mCanvas);
      color = mBitmap.getPixel(0, 0);

      colorDrawable.setBounds(mBounds); // Restore the original bounds.
    }
    else {
      color = ((ColorDrawable)view.getBackground()).getColor();
    }
  }

  return color;
}
jpmcosta
  • 1,752
  • 15
  • 24
  • I used this `((ColorDrawable) row.getBackground()).getColor()` as `(row.background as ColorDrawable).color` but i faced with this error `android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable` – milad salimi Aug 08 '19 at 06:13
  • You should not cast `row.getBackground()` to `ColorDrawable` without checking first if it is indeed a `ColorDrawable`. That's why I start with `if(view.getBackground() instanceof ColorDrawable)`. In the case of a `StateListDrawable` (a `DrawableContainer`) you might need to work with `drawable.getCurrent()`. However, that alone might not suffice. It really depends on the drawable's structure. Also, be mindful that this snippet has almost 7 years and a lot has changed since then. Good luck. – jpmcosta Aug 08 '19 at 23:24
17

You can also try something like set the color value as the tag like

android:tag="#ff0000"

And access it from the code

String colorCode = (String)btn.getTag();
blessanm86
  • 31,439
  • 14
  • 68
  • 79
10

The simpliest way to get the color for me is:

int color = ((ColorDrawable)button.getBackground()).getColor();

Tested and working on Lollipop 5.1.1

Java Geek
  • 337
  • 4
  • 6
2

To get the background Drawable, you use

public Drawable getBackground();

as defined in the base View class.

Don't forget that the Button can have a background that is an image, a color, a gradient. If you use android:background="#ffffff", the class of the background will be

android.graphics.drawable.ColorDrawable

From there you can simply call

public int getColor()
brianestey
  • 8,202
  • 5
  • 33
  • 48
0

Try this:

list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);        
ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground();
if(corItem.getColor() == Color.YELLOW){
   Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show();
   }else{
   Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show();
}

or

int color =( (ColorDrawable)  list_view.getChildAt(position).getBackground()).getColor();
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
0
int colornumber=((ColorDrawable)v.getBackground()).getColor();

This is the best and simple way to get the color of a View (Button, TextView...)

To get set color to a View using java we use

v.setBackgroundColor(getColor(R.color.colorname_you_used));
Eric Aya
  • 69,473
  • 35
  • 181
  • 253