How to programmatically set drawableRight on Android Edittext?

AndroidDrawable

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSanket KachhelaView Question on Stackoverflow
Solution 1 - AndroidLawrence ChoyView Answer on Stackoverflow
Solution 2 - AndroidRethinavelView Answer on Stackoverflow
Solution 3 - AndroidSagar MaiyadView Answer on Stackoverflow
Solution 4 - Androidmisbahm3View Answer on Stackoverflow
Solution 5 - Androidabir-cseView Answer on Stackoverflow
Solution 6 - AndroidRenjith KrishnanView Answer on Stackoverflow
Solution 7 - AndroidKeshav GeraView Answer on Stackoverflow
Solution 8 - AndroidMarGinView Answer on Stackoverflow
Solution 9 - AndroidPaul MathewView Answer on Stackoverflow