14

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

user2428118
  • 7,935
  • 4
  • 45
  • 72
Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • Is there a particular reason for why Android's built in list controls can't be used? – Premsuraj Mar 12 '13 at 08:39
  • @Premsuraj: no, can i use built in widgets for this purpose? how? – Fcoder Mar 12 '13 at 08:42
  • Why do you need multiple image views? What is the purpose? do you need to display images in a list? – Yasitha Waduge Mar 12 '13 at 08:50
  • @vmerror: i want to load several images inside a scrollviewer like instagram app. please help me in simple way – Fcoder Mar 12 '13 at 09:15
  • 2
    If thats the requirement, you need to first study android ListView with custom Adapter, in adapter getView method download the image and set it to custom xml layout. That should be the approach. – Yasitha Waduge Mar 12 '13 at 10:03
  • Best way to do this is to use a recyclerview with each viewholder being an imageview – Simon Oct 10 '15 at 08:55
  • @YasithaChinthaka That's the approach I have followed but the code I am stuck at is that it sets image url to 2-3 elements only out of 5 in my case at this moment. And, no specific solution I've located so far. Any idea towards its solution? (Please note that I need to load images from URLs stored in database and I am using GridView & custom xml layout for this.) – meDeepakJain May 05 '19 at 09:41

4 Answers4

28

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);
}
BlazE
  • 81
  • 1
  • 16
1

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 image views to.

For more customization check out the dev page, where all the possible options are listed.

Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
0

If your requirement is show in List or Gridview then you should go for Lazy Loading List or grid.

Please go through the below link.

https://github.com/thest1/LazyList

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

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

Try this

 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);
    }