android - show compound drawable to EditText on event -


in application in activity want set edittext click inside(focus) edittext , type key clear button should appear on right side of edittext
, when edittext empty clear button have removed.
but not showing me..
which event should have implement here..?ontouch or onfocuschange or addtextchangedlistener , code there..? following code have done in activity...

in activity :

 clear = getresources().getdrawable(r.drawable.round_clear);      clear.setbounds(0, 0, clear.getintrinsicwidth(), clear.getintrinsicheight()); 

and event

@override public void onfocuschange(view v, boolean hasfocus)  {     switch (v.getid())      {         case r.id.uidedittext:                 if(hasfocus && !uidedittext.gettext().tostring().isempty())                      uidedittext.setcompounddrawables(null, null, clear, null);                                  else                      uidedittext.setcompounddrawables(null, null, null, null);             break;          case r.id.pwdedittext:                 if(hasfocus && !pwdedittext.gettext().tostring().isempty())                     pwdedittext.setcompounddrawables(null, null, clear, null);                 else                     pwdedittext.setcompounddrawables(null, null, null, null);              break;               }                } 


another event :


@override public boolean ontouch(view v, motionevent event)  {     switch (v.getid())      {         case r.id.uidedittext:             final int x = (int)event.getx();             final int y = (int)event.gety();                           if(event.getaction() == motionevent.action_up && clear!=null) {                 rect rbounds = clear.getbounds();                  int n1 = v.getright();                 int n2 = v.getright()+rbounds.width();                 int n3 = v.getpaddingtop();                 int n4 = v.getheight()-v.getpaddingbottom();                  if(x>=(n1) && x<=(n2) && y>=n3 && y<=(n4))                 {                     uidedittext.settext("");                     event.setaction(motionevent.action_cancel);                 }             }     break;        }      } 

i sovled it...created following code

public class customedittext extends edittext {  private drawable  dright; private rect rbounds; customedittext(context context,attributeset attributeset){     super(context,attributeset); } @override public void setcompounddrawables(drawable left, drawable top,         drawable right, drawable bottom) {     /*if (left != null) {         dleft = left;     }*/     if (right != null) {         dright = right;     }     super.setcompounddrawables(left, top, right, bottom); }  @override public void addtextchangedlistener(textwatcher watcher) {            super.addtextchangedlistener(watcher);       }  @override protected void ontextchanged(charsequence text, int start,         int lengthbefore, int lengthafter) {             super.ontextchanged(text, start, lengthbefore, lengthafter);     if(this.gettext().tostring().length()>0)         this.setcompounddrawableswithintrinsicbounds(null, null, dright, null);     else         this.setcompounddrawableswithintrinsicbounds(null, null, null, null); }    @override protected void finalize() throws throwable {     dright = null;     rbounds = null;     super.finalize(); }  } 

and added in xml:

<com.example.screen.customedittext     android:id="@+id/uidedittext"     android:layout_width="wrap_content"     android:layout_height="wrap_content"            android:drawableright="@drawable/round_clear"            android:textcolor="#ffffff" /> 


in activity (edittext ontouch listener):

 uidedittext.setontouchlistener(new ontouchlistener() {                  @override         public boolean ontouch(view v, motionevent event) {             if (event.getaction() != motionevent.action_up)                                          return false;              if (event.getx() > uidedittext.getwidth()  - clear.getintrinsicwidth())              {                 uidedittext.settext("");                     event.setaction(motionevent.action_cancel);             }             return false;            }     }); 

Comments