Search code examples
androidimagehorizontalscrollview

Adding multiple images to horizontal scroll view in android


enter image description hereI am building an application in which I want to add multiple images in a horizontal scrollview and want to scroll these imageson click of two (left and right) srcolling buttons, but I am not able to do that as I am new to android development.So can anybody provide my with the solution how to do it ? Thanks in advance....


Solution

  • put this code in your xml

    <HorizontalScrollView
            android:id="@+id/xml_full_img_hor_below_view"
            android:layout_width="match_parent"
            android:layout_height="@dimen/full_img_below_relative_height"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center"
            android:orientation="horizontal" >
    
            <LinearLayout
                android:id="@+id/xml_full_img_linear_below_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:orientation="horizontal" >
            </LinearLayout>
        </HorizontalScrollView>
    

    ///////////////////////////////////// java code

    this model_ProductMaster.listProductImages.size() is images list:

    private void setProductImageInView() {
    
            LayoutInflater layoutInflater = (LayoutInflater) viewFullImage
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            linear_below_img_view.removeAllViews();
            // Set the image height,widt,scaling and margin of the imageview
            for (int i = 0; i < model_ProductMaster.listProductImages.size(); i++) {
                convertView = layoutInflater.inflate(
                        R.layout.xml_row_full_image_view, null, false);
                final ImageView img = (ImageView) convertView
                        .findViewById(R.id.xml_full_img_below_middleimg);
                img.setMinimumWidth((int) viewFullImage.getResources()
                        .getDimension(R.dimen.full_img_below_img_width_height));
                img.setMaxWidth((int) viewFullImage.getResources().getDimension(
                        R.dimen.full_img_below_img_width_height));
                img.setMinimumHeight((int) viewFullImage.getResources()
                        .getDimension(R.dimen.full_img_below_img_width_height));
                img.setMaxHeight((int) viewFullImage.getResources().getDimension(
                        R.dimen.full_img_below_img_width_height));
                img.setScaleType(ScaleType.CENTER_INSIDE);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(10, 0, 0, 0);
                layoutParams.gravity = Gravity.CENTER;
                convertView.setLayoutParams(layoutParams);
                img.setScaleType(ScaleType.FIT_XY);
                imageLoader.displayImage(
                        model_ProductMaster.listProductImages.get(i).ImageUrl, img,
                        options, animateFirstListener);
                linear_below_img_view.addView(convertView);
                convertView.setId(i);
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < linear_below_img_view.getChildCount(); i++) {
                            View view = linear_below_img_view.getChildAt(i);
                            if (view == v) {
                                view.setBackgroundResource(R.drawable.xml_popup_border_with_bg);
                            } else {
                                view.setBackgroundResource(R.drawable.common_bg_with_dark_border);
                            }
                        }
    
                        loadImageonCenter(model_ProductMaster.listProductImages
                                .get(v.getId()).ImageUrl);
                        System.gc();
                    }
                });
    
            }
            linear_below_img_view.getChildAt(0).setBackgroundResource(
                    R.drawable.xml_popup_border_with_bg);
            // Image Starting load
    
            System.gc();
        }