Question Detail

How to programmatically set drawableRight on Android TextView?

6 years ago Views 3051 Visit Post Reply

I required doing DrawableRight programmatically because it changes as per some condition.

<TextView
    android:id="@+id/fcoin_TextView_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawablePadding="@dimen/btn_margin"
    android:drawableRight="@drawable/fccsmall"
    android:gravity="center|left"
    android:text="00" />

I can use this but need to change in every condition:

I have tried Something like below code but not Succeed. Please Correct me 

fc_TextView.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_coin),null,null,null);


Thread Reply

Correct Answer

Hemant Sharma

- 6 years ago

You need to use CompoundDrawablesWithIntrinsicBounds

fc_TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_coin, 0);