Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ImageViews dynamically inside a loop

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

like image 543
Fcoder Avatar asked Mar 12 '13 08:03

Fcoder


1 Answers

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);
}
like image 77
Hussain Akhtar Wahid 'Ghouri' Avatar answered Sep 22 '22 06:09

Hussain Akhtar Wahid 'Ghouri'