Create ImageViews dynamically inside a loop

android, imageview, java

Solution

you can modify the layout , image resource and no of images (may be dynamic as well) 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);
}

Problem

I have written this code that loads an image to in 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 load several images. for this I need create image views dynamically but i don't know how... I want 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 creating several ImageViews inside a loop dynamically

Original source

Related problems