How to programmatically set drawableRight on Android Edittext?

Sanket Kachhela picture Sanket Kachhela · Mar 10, 2014 · Viewed 73.6k times · Source

I know about set drawableRight in XML. but i required to do it programmatically because it is change as per some condition.

Answer

Lawrence Choy picture Lawrence Choy · Mar 10, 2014

You can use the function below:

editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.drawableRight, 0);

or (if you want to pass the drawable itself instead of its ID)

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context,R.drawable.drawableRight), null)

The order of params corresponding to the drawable location is: left, top, right, bottom