How to programmatically set drawableRight on Android Edittext? How to programmatically set drawableRight on Android Edittext? android android

How to programmatically set drawableRight on Android Edittext?


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


Find Further here

EditText myEdit = (EditText) findViewById(R.id.myEdit);myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon, 0);  // where params are (left,top,right,bottom)

You can also set drawable padding programmatically:

myEdit.setCompoundDrawablePadding("Padding value");


Try like below:

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );EdtText.setCompoundDrawablesWithIntrinsicBounds( 0, 0, img, 0);

Edit :

 int img = R.drawable.smiley; EdtText.setCompoundDrawablesWithIntrinsicBounds( 0, 0, img, 0);