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 ?
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 }
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];
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With