android - Button Highlight - how to apply onTouch to 2 buttons -


i have 2 buttons this:

button b = new button(this);         b.settext("button onw");         b.setid(1111);         b.setbackgrounddrawable(r.drawable.button);         b.setontouchlistener(listenerone);         rootlayout.addview(b);          button b1 = new button(this);         b1.settext("button two");         b1.setbackgrounddrawable(r.drawable.button);         b1.setontouchlistener(listenerone);         relativelayout.layoutparams = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, uiutils.convertdptopixels(activity             .getresources().getdisplaymetrics(), 45));         i.addrule(relativelayout.below, b.getid());         rootlayout.addview(b1, i); 

now ontouch :

ontouchlistener listenerone = new ontouchlistener() {     @override     public boolean ontouch(view view, motionevent event) {         switch (event.getaction()) {         case motionevent.action_down:          view.getbackground().setcolorfilter(color.parsecolor("#b7b2b0"), porterduff.mode.multiply);         view.invalidate();         break;         case motionevent.action_cancel:         view.getbackground().clearcolorfilter();         view.invalidate();         break;         case motionevent.action_up:         view.getbackground().clearcolorfilter();         view.invalidate();         break;         }         return false;     }     }; 

now problem here when touch 1 button 2nd button gets highlighted ? why whats wrong here?

use anonymous inner class below

    b.setontouchlistener(new ontouchlistener()     {      public boolean ontouch(view arg0, motionevent event) {             // todo auto-generated method stub     switch(event.getaction())     {         case motionevent.action_down:              b.setbackgroundcolor(color.blue);             break;         case motionevent.action_up:              b.setbackgroundcolor(color.green);             break;         }              return true;     }      }); 

similarly button b1

    b1.setontouchlistener(new ontouchlistener()     {      public boolean ontouch(view arg0, motionevent event) {             // todo auto-generated method stub     switch(event.getaction())     {         case motionevent.action_down:              b1.setbackgroundcolor(color.blue);             break;         case motionevent.action_up:              b1.setbackgroundcolor(color.green);             break;         }              return true;     }      }); 

edit: used above code. works fine. when ever button in touched ondown color changes blue , on changes green.

            b1.setontouchlistener(listenerone);     b.setontouchlistener(listenerone);         

and listener

             ontouchlistener listenerone = new ontouchlistener() {     @override     public boolean ontouch(view view, motionevent event) {         switch (event.getaction()) {         case motionevent.action_down:              view.setbackgroundcolor(color.blue);             break;         case motionevent.action_up:              view.setbackgroundcolor(color.green);           break;         }         return false;     }     };           

edit 2:

          b.setbackgroundcolor(color.green);//default background  color green       b1.setbackgroundcolor(color.green); // default background  color green 

use code in edit or anonymous inner class

enter image description here

on touch down

enter image description here

on release button background green again


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -