75

I want to find the background color of a Layout from my code. Is there any way to find it? something like linearLayout.getBackgroundColor()?

Srujan Simha
  • 3,637
  • 8
  • 42
  • 59
  • 2
    Since the background may not be a color, you can use linearLayout.getBackground() which will give you a `Drawable`. There is no API to get background color, specifically. [Read more in the docs for View](http://developer.android.com/reference/android/view/View.html#getBackground%28%29) – Simon Forsberg Feb 08 '13 at 18:47
  • But I really need to find the color of a layout. There should be some other way ! or is it possible to get it from `Drawable`? – Srujan Simha Feb 08 '13 at 18:53
  • Why are you not [pulling the background color from the theme](https://stackoverflow.com/a/14468034/712526)? – jpaugh Jan 04 '21 at 06:23

6 Answers6

152

This can only be accomplished in API 11+ if your background is a solid color.

int color = Color.TRANSPARENT;
Drawable background = view.getBackground();
if (background instanceof ColorDrawable)
    color = ((ColorDrawable) background).getColor();
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Rich
  • 36,270
  • 31
  • 115
  • 154
  • I was just going to edit my answer and say that specifically that might work! However, I am not sure why there's an API 11+ restriction? `ColorDrawable` seems to be available since API 1, and also view.getBackground(). – Simon Forsberg Feb 08 '13 at 18:58
  • Nevermind. I see that `.getColor` for ColorDrawable was added in API 11. – Simon Forsberg Feb 08 '13 at 19:01
  • 1
    You could convert the `Drawable` to a `Bitmap` and get the first pixel. `int color = bitmap.getPixel(0, 0);` – Jared Rummler Jan 15 '15 at 04:11
  • 1
    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 05:59
19

To get background color of a Layout:

LinearLayout lay = (LinearLayout) findViewById(R.id.lay1);
ColorDrawable viewColor = (ColorDrawable) lay.getBackground();
int colorId = viewColor.getColor();

If It is RelativeLayout then just find its id and use there object instead of LinearLayout.

arpit
  • 1,462
  • 1
  • 12
  • 12
13

ColorDrawable.getColor() will only work with API level above 11, so you can use this code to support it from API level 1. Use reflection below API level 11.

public static int getBackgroundColor(View view) {
        Drawable drawable = view.getBackground();
        if (drawable instanceof ColorDrawable) {
            ColorDrawable colorDrawable = (ColorDrawable) drawable;
            if (Build.VERSION.SDK_INT >= 11) {
                return colorDrawable.getColor();
            }
            try {
                Field field = colorDrawable.getClass().getDeclaredField("mState");
                field.setAccessible(true);
                Object object = field.get(colorDrawable);
                field = object.getClass().getDeclaredField("mUseColor");
                field.setAccessible(true);
                return field.getInt(object);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
Akhil Dad
  • 1,804
  • 22
  • 35
9

Short and Simple way:

int color = ((ColorDrawable)view.getBackground()).getColor();
Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
6

For kotlin fans

fun View.getBackgroundColor() = (background as? ColorDrawable?)?.color ?: Color.TRANSPARENT
Dominik Suszczewicz
  • 1,469
  • 2
  • 16
  • 23
0

I think there are cases where background might not a ColorDrawable so we need to check it before the cast:

 if (view.background is ColorDrawable) {
     val bgColor = (view.background as ColorDrawable).color
 }
Raphael C
  • 2,296
  • 1
  • 22
  • 22