Skip to content Skip to sidebar Skip to footer

How To Programmatically Set Drawableright On Android Edittext?

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

Solution 1:

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

Solution 2:

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");

Solution 3:

Try like below:

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

Edit :

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

Solution 4:

Try:

EditTexteditFirstname= (EditText) findViewById(R.id.edit_fname);
DrawableicUser= getResources().getDrawable(R.drawable.ic_user);
editFirstname.setCompoundDrawablesWithIntrinsicBounds(null, null, icUser, null);

Then you can add a touch listener to that specific drawable.

Solution 5:

For changing left and right both at a time I use this single line.

download.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_lock_open_white_24dp, 0, R.drawable.ic_lock_open_white_24dp, 0);

Post a Comment for "How To Programmatically Set Drawableright On Android Edittext?"