101

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75

9 Answers9

291

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

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
Lawrence Choy
  • 6,088
  • 1
  • 20
  • 30
  • 1
    how can I assign an ID to that drawable ? basically I want to add a touch listener to that specific drawable – Thorvald Dec 08 '16 at 04:01
  • @Lawrence Choy hi sir,how to check Image is already exit or not.Please help me in this case,And how to change image color. – Gowthaman M Jul 24 '17 at 07:38
10

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");
androidevil
  • 9,011
  • 14
  • 41
  • 79
Rethinavel
  • 3,912
  • 7
  • 28
  • 49
4

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);
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
4

Try:

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

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

Rafael Costa
  • 1,291
  • 2
  • 13
  • 30
misbahm3
  • 101
  • 5
3

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);
abir-cse
  • 507
  • 3
  • 12
2
int img = R.drawable.smiley;
editText.setCompoundDrawables( null, null, img, null );

Explained here

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

androidevil
  • 9,011
  • 14
  • 41
  • 79
Renjith Krishnan
  • 2,446
  • 5
  • 29
  • 53
2
        et_feedback.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                }
              et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.feedback_new, 0, 0);                
               et_feedback.setTextColor(Color.BLACK);

            }
        });

Hide Drawable using this

et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
2

If it requires android graphics drawable then this will work

Drawable dw = getApplicationContext().getResources().getDrawable(R.drawable.edit);
Button start = (Button)findViewById(R.id.buttonStart);
start.setCompoundDrawablesWithIntrinsicBounds(dw, null, null, null);
MarGin
  • 2,078
  • 1
  • 17
  • 28
1

You can use your editText view (here it is txview) built in function setCompoundDrawablesWithIntrinsicBounds() to do what you are looking for

in my code I Used it like this . txview.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_arrow_drop_up,0);

txview.setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom);