button - Android onClickListener without xml -


here code:

... setcontentview(r.layout.activity_main);         mygrid= (gridlayout) findviewbyid(r.id.gridlayout);         mygrid.setcolumncount(3);         mygrid.setrowcount(5);          imagebutton a1 = new imagebutton(this);     imagebutton a2 = new imagebutton(this);     mygrid.addview(a1);     mygrid.addview(a2);     a1.setonclicklistener(this);     a2.setonclicklistener(this); ... 

what need write in onclick method? want if press button "a1" , if press button "a2" another.

public void onclick(view a) {    can't use "if (a.getid == buttonid)" , because buttons haven't id.     } 

in simple java if((a.getsource() == button1)), don't know how looks in android.

in android, need override onclick method in listener:

a1.setonclicklistener(new onclicklistener(){    @override    public void onclick(view view){        //    }  }); 

or create custom listener 1 both buttons, check view clicked:

myonclicklistener = new onclicklistener(){     @override     public void onclick(view view){         if (view == a1){            // a1 should         } else if (view == a2) {            // a2 should         }     } }; 

and append both buttons:

a1.setonclicklistener(myonclicklistener); a2.setonclicklistener(myonclicklistener); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -