android - How to set the image resource of a dynamic imageview using multiple buttons -
hi i'm new programming , android development. i'm working on project , hoping can me.
i have 2 activities 1 monthselect, contains buttons 1 each month of year. other activity quotedisplay, contains dynamic imageview, array list of images , swipe gesture detector.
i'm trying make each button open imageview on different image array list , allow me scroll through images normal using swipe gestures.
for example if february button pressed imageview displays first image of february , can swipe left second image in february. or if may button pressed imageview displays first image of may , allows me scrolls through images please.
iv tried using intents different info in each make unique read somewhere saying work hasn't activity keep opening same image. unfortunately don't know other methods , learning thought task figure out.
i have included simplified version of code if can i'd appreciate much.
monthselect class
public class monthselect extends activity { //imageview images; //imageview imagestest; button jan; button feb; button march; button april; button may; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.month_select); setrequestedorientation(activityinfo.screen_orientation_landscape); findviewbyid(r.id.januarybutton).setonclicklistener(buttonclicklistener); findviewbyid(r.id.februarybutton).setonclicklistener(buttonclicklistener); findviewbyid(r.id.marchbutton).setonclicklistener(buttonclicklistener); findviewbyid(r.id.aprilbutton).setonclicklistener(buttonclicklistener); findviewbyid(r.id.maybutton).setonclicklistener(buttonclicklistener); } final onclicklistener buttonclicklistener = new onclicklistener(){ @override public void onclick(view v) { //images = (imageview)findviewbyid(r.id.monthimagedisplay); //imagestest = (imageview)findviewbyid(r.id.imagestest); switch(v.getid()){ case r.id.januarybutton: intent janintent = new intent(monthselect.this,monthdisplay.class); //testing see if pressed toast.maketext(getapplicationcontext(),"january", toast.length_short).show(); break; case r.id.februarybutton: //testing see if pressed toast.maketext(getapplicationcontext(), "february", toast.length_short).show(); break; case r.id.marchbutton: //testing see if pressed toast.maketext(getapplicationcontext(), "march", toast.length_short).show(); break; case r.id.aprilbutton: //testing see if pressed toast.maketext(getapplicationcontext(), "april", toast.length_short).show(); break; case r.id.maybutton: //testing see if pressed toast.maketext(getapplicationcontext(), "may", toast.length_short).show(); break; }//switch }//onclick };//buttonclicklistener }
quotedisplay
public class quotedisplay extends activity implements ongesturelistener { relativelayout r1; private imageview imageview; textview textview; gesturedetector detector; calendar cal = calendar.getinstance(); @suppresslint("simpledateformat") simpledateformat sdf = new simpledateformat("ddd"); string strdate = sdf.format(cal.gettime()); int parsestrdate = integer.parseint(strdate); int imagearraycorrection = (parsestrdate -1); private int currentimage = imagearraycorrection; //list of images public int[] imagelist = {r.drawable.j_1, r.drawable.j_2, r.drawable.j_3, r.drawable.j_4, r.drawable.j_5, r.drawable.j_6, r.drawable.j_7, r.drawable.j_8, r.drawable.j_9, r.drawable.j_10, r.drawable.j_11, r.drawable.j_12, r.drawable.j_13, r.drawable.j_14, r.drawable.j_15, r.drawable.j_16, r.drawable.j_17, r.drawable.j_18, r.drawable.j_19, r.drawable.j_20, r.drawable.j_21, r.drawable.j_22, r.drawable.j_23, r.drawable.j_24, r.drawable.j_25, r.drawable.j_26, r.drawable.j_27, r.drawable.j_28, r.drawable.j_29, r.drawable.j_30, r.drawable.j_31, r.drawable.f_1, r.drawable.f_2, r.drawable.f_3, r.drawable.f_4, r.drawable.f_5, r.drawable.f_6, r.drawable.f_7, r.drawable.f_8, r.drawable.f_9, r.drawable.f_10, r.drawable.f_11, r.drawable.f_12, r.drawable.f_13, r.drawable.f_14, r.drawable.f_15, r.drawable.f_16, r.drawable.f_17, r.drawable.f_18, r.drawable.f_19, r.drawable.f_20, r.drawable.f_21, r.drawable.f_22, r.drawable.f_23, r.drawable.f_24, r.drawable.f_25, r.drawable.f_26, r.drawable.f_27, r.drawable.f_28, }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.quote_display); detector = new gesturedetector(this, this); imageview(); }//oncreate //dynamic imageview protected void imageview(){ r1 = (relativelayout)findviewbyid(r.id.r1); imageview = new imageview (this); imageview.setimageresource(imagelist[imagearraycorrection]); //setting parameters of relative layout relativelayout.layoutparams params = new relativelayout.layoutparams( (int)layoutparams.match_parent, (int)layoutparams.match_parent); params.addrule(relativelayout.center_in_parent); imageview.setlayoutparams(params); r1.addview(imageview); }//imageview //registering touchevent gersturedetector public boolean ontouchevent(motionevent event){ return detector.ontouchevent(event); } @override public boolean ondown(motionevent e) { // todo auto-generated method stub return false; } @override//swipe gestures left , right public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { float sensitivity = 50; //swipe left check if(e1.getx() - e2.getx()>sensitivity){ //increase counter move next image currentimage++; currentimage = (currentimage + imagelist.length)%imagelist.length; imageview.setimageresource(imagelist[currentimage]); //testing fling gesture // toast.maketext(getapplicationcontext(), "<- fling gesture left", 100).show(); return true; } //swipe right check if(e2.getx() - e1.getx()>sensitivity){ //decrease counter go image currentimage--; currentimage = (currentimage + imagelist.length)%imagelist.length; imageview.setimageresource(imagelist[currentimage]); //testing fling gesture // toast.maketext(getapplicationcontext(), "fling gesture right ->", 100).show(); } return true; }
i make 12 int-arrays picture-paths each month. in monthselect put on intent item. can read here how it. when button pressed, create new intent. depending on month selected put right intent. example january:
intent intent = new intent(monthselect.this,monthdisplay.class); //... intent.putextra("month", 1); // '1' because january first month
then in quotedisplay's oncreate method store intent-item called "month" on attribute:
int selectedmonth = getintent().getextras().getint("month");
now know selected month , can use right picture-array imageview.
Comments
Post a Comment