How to programmatically change TextView Drawable in Android

0

How to programmatically change TextView Drawable in Android is part of an issues I have solved in one of my projects. So I have decided to document it for future reference.

It might also help someone.

Question is closed for new answers.
Selected answer as best
0
Drawable topIcon = getResources().getDrawable( R.drawable.ic_favorite_border_white_24dp);
TextView.setCompoundDrawablesWithIntrinsicBounds(null, topIcon, null, null);

The image resource in the drawable folder is converted to a Drawable object.

The drawable object is passed as a parameter to TextView setCompoundDrawablesWithIntrinsicBound(left, top, right, bottom) method.

You can pass null value to drawable positions you are not interested in.

Changed status to publish