I know about set drawableRight in XML. but i required to do it programmatically because it is change as per some condition.
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.
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)
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With