-1

I want to create 600 imageview at runtime and add it to linear layout at runtime.It cause block my user interface. My activity appear when all imageview created and added to linear layout. How to resolve this.

Please help for this.

       for(int index = 0; index < ProductItemArray.Image_URL.length; index++)
        {
            ImageView bottomImageView = new ImageView(context);
            bottomImageView.setTag(index);

            if(Helper.isTablet(context))
                bottomImageView.setLayoutParams(new Gallery.LayoutParams(VirtualMirrorActivity.convertDpToPixel(100, context), VirtualMirrorActivity.convertDpToPixel(100, context)));
            else
                bottomImageView.setLayoutParams(new Gallery.LayoutParams(VirtualMirrorActivity.convertDpToPixel(80, context), VirtualMirrorActivity.convertDpToPixel(80, context)));

            UrlImageViewHelper.setUrlDrawable(bottomImageView, ProductItemArray.Image_URL[index]);
            bottomImageView.setBackgroundResource(R.layout.border);
            linearLayout3.addView(bottomImageView);
            bottomImageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int position = (Integer) v.getTag();
                    linearLayout.removeAllViews();
                    Thread newThread = new Thread(new Runnable() {
                        public void run() {
                            isAlreadyExistInWishlist = true;
                            URL url_1 = null;
                            try {
                                VMProductListPaging.productUrl = ProductItemArray.Image_small_URL[position];
                                VMProductListPaging.productId = ProductItemArray.productId[position];
                                VMProductListPaging.productName = ProductItemArray.product_Name[position];

                                url_1 = new URL(ProductItemArray.Image_small_URL[position]);
                                bmp = BitmapFactory.decodeStream(url_1.openConnection().getInputStream());
                                isExecuted = true;
                                bitmapModelsHandler.sendMessage(bitmapModelsHandler.obtainMessage());
                            }
                            catch (Exception e) {
                                //Toast.makeText(context,"Sorry!! This link appears to be broken",Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                    newThread.start();
                }
            });
        }
Ajay S
  • 45,716
  • 27
  • 84
  • 103
  • What does `UrlImageViewHelper.setUrlDrawable` do ? – sdabet Nov 20 '12 at 08:16
  • @fiddler it load image from net. – Ajay S Nov 20 '12 at 08:17
  • 600 `ImageViews` at one moment? If yes this will not works as you're creating to many views. – user Nov 20 '12 at 08:18
  • you should use lazy load to show images. please refer this [URL](http://codehenge.net/blog/2011/06/android-development-tutorial-asynchronous-lazy-loading-and-caching-of-listview-images/) – Talha Nov 20 '12 at 08:23
  • how to add adapter on linear layout – Ajay S Nov 20 '12 at 08:26
  • You can not. You have to use listview, gridview ... – Talha Nov 20 '12 at 08:32
  • but i want imageview horizontally which layout should i use – Ajay S Nov 20 '12 at 08:33
  • Look at this post, it is a good tuorial,i think it helps to you . http://androidtrainningcenter.blogspot.com/2012/04/how-to-create-simple-android-gallery.html – Talha Nov 20 '12 at 08:38
  • @talhakosen I think Gallery is not supported anymore – sdabet Nov 20 '12 at 08:45
  • @fiddler Really, if it is, what we will use instead of gallery ? – Talha Nov 20 '12 at 08:47
  • They say "This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library." [here](http://developer.android.com/reference/android/widget/Gallery.html) – sdabet Nov 20 '12 at 08:48
  • I see, thanks for your information, because of your info i give you 40 rep bounty :) – Talha Nov 20 '12 at 08:50

1 Answers1

4

Having 600 images in memory in the same time is probably not a good idea.

You should consider using some lazy loading via an adapter (with a ListView, a Gallery, a GridView, a Spinner,etc...) which will manage recycling/releasing of views.

sdabet
  • 17,379
  • 11
  • 73
  • 143