java - Selecting no of images using multiselectgrid in Android -


i have designed app have used mulitiselectgridview image selection.my requirement want restrict user select max 8 images, means whenever user try chek on 9th element should display warning message popup i.e max 8 images can selected , 9th images should autometically unchecked.

public class multiimagepicactivity extends activity {  gridview mgrid;   private string[] arrpath,modifiedarraypath; private int ids[]; private int count; int selectcount; activity act=this; list<string> imagepaths; int maximageselection; //int sel=0; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     //now intent extras         imagepaths=new arraylist<string>();     imagepaths.clear();     loadapps();      setcontentview(r.layout.grid_1);     mgrid = (gridview) findviewbyid(r.id.mygrid);      mgrid.setadapter(new imageadapterxtends());      mgrid.setchoicemode(gridview.choice_mode_multiple_modal);     mgrid.setmultichoicemodelistener(new multichoicemodelistener());  }      private void loadapps() {      final string[] columns = { mediastore.images.media.data,      mediastore.images.media._id };     final string orderby = mediastore.images.media._id;     cursor imagecursor = getcontentresolver().query(mediastore.images.media.external_content_uri, columns, null, null, orderby);     int image_column_index = imagecursor.getcolumnindex(mediastore.images.media._id);     this.count = imagecursor.getcount();     this.arrpath = new string[this.count];     ids = new int[count];     (int = 0; <this.count; i++) {         imagecursor.movetoposition(i);         ids[i] = imagecursor.getint(image_column_index);         int datacolumnindex = imagecursor.getcolumnindex(mediastore.images.media.data);         arrpath[i] = imagecursor.getstring(datacolumnindex);     }      imagecursor.close();  } @override public void onbackpressed() {     setresult(activity.result_canceled);     super.onbackpressed();  } public class imageadapterxtends extends baseadapter{     checkablelayout l;     imageview i;     @override     public int getcount() {         // todo auto-generated method stub         return arrpath.length;     }      @override     public object getitem(int position) {         // todo auto-generated method stub         return arrpath[position];     }      @override     public long getitemid(int position) {         // todo auto-generated method stub         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         // todo auto-generated method stub         if (convertview == null) {             = new imageview(multiimagepicactivity.this);             i.setscaletype(imageview.scaletype.fit_center);             i.setlayoutparams(new viewgroup.layoutparams(50, 50));             l = new checkablelayout(multiimagepicactivity.this);             l.setlayoutparams(new gridview.layoutparams(                     gridview.layoutparams.wrap_content,                     gridview.layoutparams.wrap_content));             l.addview(i);         } else {             l = (checkablelayout) convertview;             = (imageview) l.getchildat(0);         } //          bitmap bmp =      utility.getresizedbitmap(bitmapfactory.decodefile(arrpath[position]), 50, 50); //          i.setimagebitmap(bmp);         try {             setbitmap(i, ids[position]);         } catch (throwable e) {         }         return l;      }       }      private void setbitmap(final imageview iv,final int id) {          new asynctask<void, void, bitmap>() {             bitmap mybitmap;             @override             protected bitmap doinbackground(void... params) {                 return mediastore.images.thumbnails.getthumbnail(getapplicationcontext().getcontentresolver(), id, mediastore.images.thumbnails.micro_kind, null);             }              @override             protected void onpostexecute(bitmap result) {                 super.onpostexecute(result);                 //iv.setimagebitmap(result);                 setmybitmap(result);             }             public final void setmybitmap(bitmap bitmap) {                    if (this.mybitmap != null) {                     this.mybitmap.recycle();                   }                   this.mybitmap = bitmap;                   iv.getlayoutparams().height = 100;                   iv.getlayoutparams().width = 100;                   iv.setimagebitmap(mybitmap);                 }         }.execute();     }      public class checkablelayout extends framelayout implements checkable {     private boolean mchecked;      public checkablelayout(context context) {         super(context);     }      public void setchecked(boolean checked) {          mchecked = checked;         setforeground(checked ? getresources().getdrawable(r.drawable.tr) : null);         }      public boolean ischecked() {         return mchecked;     }      public void toggle() {         //sel--;         selectcount--;         setchecked(!mchecked);     }  }  public class multichoicemodelistener implements         gridview.multichoicemodelistener {     public boolean oncreateactionmode(actionmode mode, menu menu) {         // inflate menu cab         menuinflater inflater = mode.getmenuinflater();         inflater.inflate(r.menu.context_menu, menu);         //return true;         mode.settitle("select items");         mode.setsubtitle("one item selected");         return true;     }      public boolean onprepareactionmode(actionmode mode, menu menu) {         return true;     }      public boolean onactionitemclicked(actionmode mode, menuitem item) {         // respond clicks on actions in cab         switch (item.getitemid()) {             case r.id.menu_ok:             intent = new intent();                 modifiedarraypath=imagepaths.toarray(new string[imagepaths.size()]);                 i.putextra("selectedimagepath", modifiedarraypath);                 //i.putextra("data", selectedpath);                 setresult(activity.result_ok, i);                 //toast.maketext(getapplicationcontext(),string.valueof(a),toast.length_long).show();                  //todo stuff here                 //mode.finish(); // action picked, close cab                 act.finish();                 return true;             default:                 return false;         }     }      public void ondestroyactionmode(actionmode mode) {         toast.maketext(getapplicationcontext(), "text", 1);     }      public void onitemcheckedstatechanged(actionmode mode, int position,             long id, boolean checked) {         selectcount = mgrid.getcheckeditemcount();         if(checked){             imagepaths.add(arrpath[position]);             if(selectcount>8){                 imagepaths.remove(arrpath[position]);                 new alertdialog.builder(multiimagepicactivity.this)                 .settitle("restriction")                 .setmessage("max 8 images allowed!!!")                 .setpositivebutton(android.r.string.yes, new         dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                          // continue delete                         //finish();                         dialog.dismiss();                     }                  })                 .seticon(android.r.drawable.ic_dialog_alert)                  .show();                  selectcount=maximageselection;             }         switch (selectcount) {         case 1:             mode.setsubtitle("one item selected");             break;         default:             mode.setsubtitle("" + selectcount + " items selected");             break;         }         }         else             imagepaths.remove(arrpath[position]);     }  } } 


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -