Get background color of a Layout
Asked Answered
D

6

78

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

Deductive answered 8/2, 2013 at 18:44 Comment(3)
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 ViewGas
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?Deductive
Why are you not pulling the background color from the theme?Abysmal
E
154

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();
Economize answered 8/2, 2013 at 18:57 Comment(4)
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().Gas
Nevermind. I see that .getColor for ColorDrawable was added in API 11.Gas
You could convert the Drawable to a Bitmap and get the first pixel. int color = bitmap.getPixel(0, 0);Regin
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.ColorDrawableEnidenigma
H
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.

Haphtarah answered 17/8, 2015 at 7:27 Comment(1)
Is there any reason against using ViewGroup (or even View) instead all it's possible sub classes to get the background?Preside
K
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;
    }
Katanga answered 30/7, 2015 at 2:24 Comment(0)
N
9

Short and Simple way:

int color = ((ColorDrawable)view.getBackground()).getColor();
Nannana answered 2/3, 2018 at 13:3 Comment(0)
M
7

For kotlin fans

fun View.getBackgroundColor() = (background as? ColorDrawable?)?.color ?: Color.TRANSPARENT
Morganatic answered 18/3, 2020 at 1:46 Comment(0)
R
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
 }
Rosendorosene answered 18/5, 2022 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.