Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 878
Sanket Kachhela Avatar asked Mar 10 '14 10:03

Sanket Kachhela


People also ask

How to set drawable background to EditText in Android programmatically?

setBackgroundResource() method is used to change the button background programmatically. setBackgroundResource(int id) accepts id of drawable resource and applies the background to the button.

What is SetCompoundDrawablesWithIntrinsicBounds?

SetCompoundDrawablesWithIntrinsicBounds(Drawable, Drawable, Drawable, Drawable) Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. SetCompoundDrawablesWithIntrinsicBounds(Int32, Int32, Int32, Int32)


2 Answers

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

like image 146
Lawrence Choy Avatar answered Sep 22 '22 03:09

Lawrence Choy


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"); 
like image 31
Rethinavel Avatar answered Sep 20 '22 03:09

Rethinavel