android - Multiple ImageButtons for one purpose in a fragment -


i have number of camera icons next multiple questions. way if user has take picture document answer can. takes place inside fragment. have following code

public class postinstallation extends fragment { view view;  public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {      view = inflater.inflate(r.layout.fragment_pre_installation, container,             false);      imagebutton camera = (imagebutton) view             .findviewbyid(r.id.pre_install_camera);           camera.setonclicklistener(new view.onclicklistener() {           public void onclick(view v) {             intent intent = new intent("android.media.action.image_capture");             startactivity(intent);         }      });           return view; } } 

it works beautifully, no problems. stumped on how add 10 more of these imagebuttons efficiently. have use this?

 button1.setonclicklistener(this);  button2.setonclicklistener(this);  button3.setonclicklistener(this);    @override  public void onclick(view v) {    switch(v.getid()) {        case r.id.button1:        // stuff;        break;        case r.id.button2:        // stuff;        break;    ... } } 

and if mean in fragment have call statement on , on imagebuttons?

button.setonclicklistener((onclicklistener) this.view); 

the reason ask because switch statement seems redundant seeing how these buttons exact same thing. call camera. i'm positive others have run , found more efficient/elegant solution. in advance!

working code

this ended settling on using, upvote , welcome new posts in thread done less code. i'm less code. thanks!

public class postinstallation extends fragment { view view;  public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {      view = inflater.inflate(r.layout.fragment_post_installation, container,             false);      imagebutton camera = (imagebutton) view             .findviewbyid(r.id.camera_button);     imagebutton camera2 = (imagebutton) view             .findviewbyid(r.id.camera_button2);     imagebutton camera3 = (imagebutton) view             .findviewbyid(r.id.camera_button3);     imagebutton camera4 = (imagebutton) view             .findviewbyid(r.id.camera_button4);      onclicklistener cameralistener = new onclicklistener() {                      public void onclick(view v) {             intent intent = new intent("android.media.action.image_capture");             startactivity(intent);         }      };      camera.setonclicklistener(cameralistener);     camera2.setonclicklistener(cameralistener);     camera3.setonclicklistener(cameralistener);     camera4.setonclicklistener(cameralistener);           return view; } } 

create 1 listener , add buttons.

onclicklistener cameralistener = new onclicklistener(){     public void onclick(view v){         takepicutre()     } } view.findviewbyid(r.id.button1).setonclicklistener(cameralistener); view.findviewbyid(r.id.button2).setonclicklistener(cameralistener); view.findviewbyid(r.id.button3).setonclicklistener(cameralistener); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -