Creating ImageViews Dynamically Inside a Loop

I wrote this code that loads an image into an ImageView widget:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery); i = (ImageView)findViewById(R.id.imageView1); new get_image("https://www.google.com/images/srpr/logo4w.png") { ImageView imageView1 = new ImageView(GalleryActivity.this); ProgressDialog dialog = ProgressDialog.show(GalleryActivity.this, "", "Loading. Please wait...", true); protected void onPreExecute(){ super.onPreExecute(); } protected void onPostExecute(Boolean result) { i.setImageBitmap(bitmap); dialog.dismiss(); } }.execute(); } 

bu now, I want to upload some images. for this I need to dynamically create images, but I do not know how ...

I want to run my code inside a for loop:

 for(int i;i<range;i++){ //LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY } 

My main problem is to create multiple ImageViews inside the loop dynamically

+6
source share
4 answers

you can change the layout, image resource and no images (also can be dynamic) according to your requirement ...

 LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout); for(int i=0;i<10;i++) { ImageView image = new ImageView(this); image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60)); image.setMaxHeight(20); image.setMaxWidth(20); // Adds the view to the layout layout.addView(image); } 
+14
source

You can use this code to create ImageViews.

 ImageView image = new ImageView(this); LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); image.setLayoutParams(vp); image.setMaxHeight(50); image.setMaxWidth(50); // other image settings image.setImageDrawable(drawable); theLayout.addView(image); 

where theLayout is the layout you want to add your images to.

For more customization, check out the dev page for all possible options.

+1
source

If your requirement is displayed in a list or in a Gridview, then you should go for the Lazy Loading List or grid.

Please follow the link below.

https://github.com/thest1/LazyList

https://github.com/abscondment/lazy-gallery

0
source

try it

  rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER_VERTICAL; params.setMargins(0, 0, 60, 0); for(int x=0;x<2;x++) { ImageView image = new ImageView(getActivity()); image.setBackgroundResource(R.drawable.ic_swimming); rootLayout.addView(image); } 
0
source

All Articles