Is there such a method call "getBackgroundColor"?

Is there such a method call "getBackgroundColor" in TextView? if I got 2 textViews: tv1 and tv2 in one LinearLayout. What I did:tv1.setBackgroundColor(Color.BLUE)

Now if I wanna setBackgroundColor of tv2 as the same as tv1, how can I get the backgroundColor in tv1 first and then setBackgroundColor of tv2?


You will find the solution here :

It will be something like that :

((PaintDrawable) tv.getBackground()).getPaint()

There is a better solution than bourbons:


The advantage is we get an integer which is comparable to color enums given by Color class.

Setting a background color sets a Drawable with that specified color as the background, i.e. the following example will work just fine:

public void onCreate(Bundle savedInstanceState) {
    TextView t1 = (TextView) findViewById(;
    TextView t2 = (TextView) findViewById(;


There is no such method, because in common there is now "background color" - there can be any Drawable object(for example picture). So, you should remember what color do you set for text.

If you can't save it - use View.setTag() and View.getTag() methods to store any value, associated with view.

Here is an additional option:

The way I solved this problem for my app was to define the colors in values/color.xml.


    <color name="blue">#ff0099cc</color>
    <color name="dark_grey">#ff1d1d1d</color>
    <color name="white">#ffffffff</color>

    <color name="textview_background">@color/white</color>


In the layout the TextView has:


If I want to get the background color in code I can just use:


This gives me a Color object directly without worrying about getting the color from a Drawable.

It works for me.

public static int getColor(View v) {
        return ((ColorDrawable)v.getBackground()).getColor();
        Field f=View.class.getDeclaredField("mState");
        Object mState=f.get(v);
        Field f2=mState.getClass().getDeclaredField("mUseColor");
        return (int) f2.get(mState);
       catch (Exception e)

    return 0;

Need Your Help

mailto fails in IE where there is a long body text. Is there any way to resolve this?

internet-explorer email

I am having a problem using Internet Explorer 8 (IE8) to open mailto links with long messages.

Recursive File Search in .net

file io .net file-search

I need to search a drive (C:, D: etc) for a partuicular file type (extension like .xml, .csv, .xls). How do I preform a recursive search to loop all directories and inner directories and return the...