28

i have textview

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:drawableTop="@drawable/new" />

and i want on activity jave to know which image has set on drawableTop, how ?

William Kinaan
  • 28,059
  • 20
  • 85
  • 118

4 Answers4

29

Use

 Drawable[] drawables = textView.getCompoundDrawables();

Also if you want to compare two drawable.

Bitmap bitmap = ((BitmapDrawable)drawables[1] ).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap();

if(bitmap == bitmap2)
    {
        //Code blcok
    }
Nimish Choudhary
  • 2,048
  • 18
  • 17
12

First, give your TextView an ID so you can find it in your Activity:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dip"
    android:drawableTop="@drawable/new" />

Second, in your Activity find your TextView and get the compound drawables in your onCreate method after calling setContentView:

TextView textView = (TextView) findViewById(R.id.textView);
// Left, top, right, bottom drawables.
Drawable[] compoundDrawables = textView.getCompoundDrawables();
// This is the top drawable.
Drawable topCompoundDrawable = compoundDrawables[1];
Micer
  • 8,731
  • 3
  • 79
  • 73
Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
2

I think you should use textView.getCompoundDrawables(); As for getting from drawable top i would think it would be the second drawable eg

Drawables tmp[] = textView.getCompoundDrawables();
tmp[1];  //this should be your top drawable but not 100% sure.
Raigex
  • 1,205
  • 12
  • 32
2

You cant get drawable ID in runtime. TextView uses ID only in it's constructor to load corresponding drawable. After drawable is loaded ID is gone.

In some cases there is no drawable ID at all. drawableTop can be a color rgb value.

Leonidos
  • 10,482
  • 2
  • 28
  • 37