Android show progress bar while image loading dynamically -
in 1 of project, loading images dynamic url. need show loading dialogue still images loads. loading images using async task. new android. please 1 give me little
my code looks like.
tablelayout table = (tablelayout)findviewbyid(r.id.tablelayout_1); for(integer i=0;i<2;i++){ tablerow rowp = new tablerow(this); rowp.setbackgroundcolor(color.parsecolor("#fff000")); imageview image = new imageview(this); string ed="http://www.domain.com/image.jpg"; image.settag(ed); downloadimagestask td=new downloadimagestask(); td.execute(image); rowp.addview(image); table.addview(rowp); } } private class downloadimagestask extends asynctask<imageview, void, bitmap> { imageview imageview = null; @override protected bitmap doinbackground(imageview... imageviews) { this.imageview = imageviews[0]; return download_image((string)imageview.gettag()); } @override protected void onpostexecute(bitmap result) { imageview.setimagebitmap(result); } private bitmap download_image(string url) { bitmap bmp =null; try{ url ulrn = new url(url); httpurlconnection con = (httpurlconnection)ulrn.openconnection(); inputstream = con.getinputstream(); bmp = bitmapfactory.decodestream(is); if (null != bmp) return bmp; }catch(exception e){} return bmp; } }
thanks in advance
edit
private class downloadimagestask extends asynctask<imageview, void, bitmap> { imageview imageview = null; progressdialog dialog; context context; public downloadimagestask(context context) { this.context = context; } @override protected bitmap doinbackground(imageview... imageviews) { this.imageview = imageviews[0]; return download_image((string)imageview.gettag()); } @override protected void onpostexecute(bitmap result) { imageview.setimagebitmap(result); if (dialog != null) dialog.dismiss(); } @override protected void onpreexecute() { super.onpreexecute(); dialog = progressdialog.show(context, "title","message"); } private bitmap download_image(string url) { bitmap bmp =null; try{ url ulrn = new url(url); httpurlconnection con = (httpurlconnection)ulrn.openconnection(); inputstream = con.getinputstream(); bmp = bitmapfactory.decodestream(is); if (null != bmp) return bmp; }catch(exception e){} return bmp; } }
add below lines in downloadimagetask
.
@override protected void onpreexecute() { super.onpreexecute(); dialog = progressdialog.show(context, "title", "message"); } @override protected void onpostexecute(void result) { super.onpostexecute(result); if (dialog != null) dialog.dismiss(); //rest of code }
and declare progressdialog dialog
in downloadimagetask
.
to pass context
, need create contructor that.
private class downloadimagestask extends asynctask<imageview, void, bitmap> { imageview imageview = null; progressdialog dialog; context context; public downloadimagestask(context context) { this.context = context; } //... rest of code .... }
Comments
Post a Comment