Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get the drawable that has sett as drawabletop of textview

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 ?

like image 823
William Kinaan Avatar asked Jan 24 '13 20:01

William Kinaan


4 Answers

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     } 
like image 146
Nimish Choudhary Avatar answered Oct 12 '22 01:10

Nimish Choudhary


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]; 
like image 36
Joseph Earl Avatar answered Oct 12 '22 01:10

Joseph Earl


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.
like image 44
Raigex Avatar answered Oct 12 '22 01:10

Raigex


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.

like image 20
Leonidos Avatar answered Oct 12 '22 00:10

Leonidos