15

How do I get the background color of a TextView?

When I press the TextView, I want to change the background color according to the background color that is in use.

TextView does not have a method such as:

getBackgroundResource()

Edit: I would prefer to get the resId of the background color.

glenneroo
  • 1,908
  • 5
  • 30
  • 49
OrSmolnik
  • 309
  • 1
  • 2
  • 12
  • Or I'm searching a little through the internet but it seems there no way to get such id from a xml defined color. Probably you should change your app and manage background colors programmatically, perhaps keeping trace of color changes during onClick events. – Cob013 Jun 20 '13 at 21:56
  • The color used by a given view might not have a resource ID. The res ID of the background is `R.attr.background`, which corresponds to `` in your theme or style. You can [get the color value from the theme](https://stackoverflow.com/a/14468034/712526), and compare that with the TextView's color to see whether it matches. – jpaugh Jan 04 '21 at 06:27

5 Answers5

21

If you want to get color code of the background try this:

if (textView.getBackground() instanceof ColorDrawable) {
    ColorDrawable cd = (ColorDrawable) textView.getBackground();
    int colorCode = cd.getColor();
}
Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50
  • i edit my question: how i get from this the resId of the color? – OrSmolnik Jun 20 '13 at 21:40
  • i tried this, but after the first line in your answer i get cd is null – OrSmolnik Jun 20 '13 at 21:54
  • How do you set background of your TextView? To use this code it has to be Color, not Gradient or other kind of Drawable – Gokhan Arik Jun 20 '13 at 21:55
  • it was because i didnt set background in the first appearance. i fixed thet, but now cd.getColor() returns me -1 – OrSmolnik Jun 20 '13 at 22:15
  • ok, its very strange. if i first set the background to color.black the getColor() returns me other number (not -1) but it still not the same resID color.black . my "if" is to check if the beckround is equals to white backround, so i replace "if (cd.getColor() == color.white)" to "if (cd.getColor() == -1)" . and it works! – OrSmolnik Jun 20 '13 at 22:28
  • This numbers are `constant Color IDs` of the some pre-defined colors. You can see the list here : (http://developer.android.com/reference/android/graphics/Color.html) The reason it returns -1 is that, White has -1 constant value. You will see in the list. – Gokhan Arik Jun 21 '13 at 15:25
  • 1
    Be careful: this can throw an `Exception`: `java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable` – Albert Vila Calvo Sep 12 '17 at 12:56
  • True. RippleDrawable wasn't introduced at the time I answered this question. You would have to check instance of background object before casting. The answer assumes you are using `ColorDrawable`. – Gokhan Arik Sep 12 '17 at 15:29
2

In Kotlin:

    val cd = view.background as ColorDrawable
    val colorCode = cd.color
Blücher
  • 823
  • 3
  • 9
  • 18
1

If you are using AppCompat use this:

ViewCompat.getBackgroundTintList(textView).getDefaultColor();

Side note: be careful if you cast to ColorDrawable, because it can throw a ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable.

Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
0

ANSWER:

we cant use the consts like color.red or color.white .

we need to figure it up how the

int intID = (ColorDrawable) holder.tvChoose.getBackground().getColor();

represent it and we have the fake ID of the color

OrSmolnik
  • 309
  • 1
  • 2
  • 12
0

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

 public static int getBackgroundColor(TextView textView) {
        Drawable drawable = textView.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