How to programmatically install drawableRight on an Android Edittext?

I know about drawableRight in XML. but I have to do it programmatically, because this change is in accordance with some condition.

+44
android drawable
Mar 10 '14 at 10:03
source share
7 answers

You can use the following function:

editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.drawableRight, 0); 

The order of the parameters corresponding to the available location is: left, top, right, bottom

+134
Mar 10 '14 at 10:05
source share

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 program the reverse routing programmatically:

 myEdit.setCompoundDrawablePadding("Padding value"); 
+6
Mar 10 '14 at 10:06
source share

Try the following:

 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); 
+4
Mar 10 '14 at 10:06
source share
 int img = R.drawable.smiley; editText.setCompoundDrawables( null, null, img, null ); 

Explained here

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

Sets Drawables (if any) to appear to the left of, above, to the right of the text and below it. Use 0 if you don't need Drawable. Borders Drawables will be set to their inner borders.

+2
Mar 10 '14 at 10:09
source share

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 particular one.

+2
Jan 27 '17 at 20:42 on
source share
  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 with

 et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0); 
0
Nov 01 '17 at 7:11
source share

You can use your editText view (here it is txview) built into the setCompoundDrawablesWithIntrinsicBounds () function 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); 
-one
Jul 21 '17 at 6:55
source share



All Articles