How to change image next and previous using button clicklistener in android? -
i have next , previous button
changing image. when activity launch, image comes previous activity
. utilize bundle object getting image on current activity
. 2 images utilize pass on bundle(image_a_inner , image_a_outer). 1 image overlap on sec image , set on custom view. want when image comes bundle press next button or previous button according position image change. example, images a_z alphabet. when press on d image display on activity using bundle , when press next button e image shown or when press previous button c image shown. below code.
private drawingview mdrawingview; bundle extras = getintent().getextras(); int imageres1 = extras.getint("picture1"); int imageres2 = extras.getint("picture2"); mdrawingview = (drawingview) findviewbyid(r.id.drawing_view); mdrawingview.setshape(imageres1, imageres2); btn_next = (button) findviewbyid(r.id.btn_next); // btn_next.setonclicklistener(this); btn_next.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub initializemp(); playsound.start(); if(imageres1==r.drawable.img_a_inner||imageres2==r.drawable.img_a){ mdrawingview.setshape(r.drawable.img_b_inner, r.drawable.img_b); index++; } else if(imageres1==r.drawable.img_b_inner||imageres2==r.drawable.img_b){ mdrawingview.setshape(r.drawable.img_c_inner, r.drawable.img_c); index++; } else if(imageres1==r.drawable.img_c_inner||imageres2==r.drawable.img_c){ mdrawingview.setshape(r.drawable.img_d_inner, r.drawable.img_d); index++; } else if(imageres1==r.drawable.img_d_inner||imageres2==r.drawable.img_d){ mdrawingview.setshape(r.drawable.img_e_inner, r.drawable.img_e); index++; } } }); btn_prev = (button) findviewbyid(r.id.btn_prev); // btn_prev.setonclicklistener(this); btn_prev.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub initializemp(); playsound.start(); if(imageres1==r.drawable.img_b_inner||imageres2==r.drawable.img_b){ mdrawingview.setshape(r.drawable.img_a_inner, r.drawable.img_a); index--; } else if(imageres1==r.drawable.img_c_inner||imageres2==r.drawable.img_c){ mdrawingview.setshape(r.drawable.img_b_inner, r.drawable.img_b); index--; } else if(imageres1==r.drawable.img_d_inner||imageres2==r.drawable.img_d){ mdrawingview.setshape(r.drawable.img_c_inner, r.drawable.img_c); index--; } }); }
if have lists images in advance, declare them arrays.
otherwise, can pass them using bundle getintarray() , putintarray().
now, have lists of images this,
// these can declared fellow member or static variables. int[] innerpictures = {r.drawable.image_a_inner, r.drawable.image_b_inner, ...} int[] pictures = {r.drawable.image_a, r.drawable.image_b, ...}
or
int[] innerpictures = extras.getintarray("innerpictures"); int[] pictures = extras.getintarray("pictures");
and need index of image displayed @ first time, can passed extra
int displayingindex = extra.getint("pictureindex"); // has fellow member variable utilize within of listener
so code below,
class="snippet-code-html lang-html prettyprint-override">private drawingview mdrawingview; bundle extras = getintent().getextras(); int[] innerpictures = ... int[] pictures = ... displayingindex = extra.getint("pictureindex"); mdrawingview = (drawingview) findviewbyid(r.id.drawing_view); mdrawingview.setshape(innerpictures[displayingindex], pictures[displayingindex]); btn_next = (button) findviewbyid(r.id.btn_next); btn_next.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { initializemp(); playsound.start(); if (displayingindex + 1 == innerpictures.length) return; displayingindex++; mdrawingview.setshape(innerpictures[displayingindex], pictures[displayingindex]); } }); btn_prev = (button) findviewbyid(r.id.btn_prev); btn_prev.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { initializemp(); playsound.start(); if (displayingindex == 0) return; displayingindex--; mdrawingview.setshape(innerpictures[displayingindex], pictures[displayingindex]); }); }
sorry bad indentation.
android button view
No comments:
Post a Comment