Hamidreza Salehi
Hamidreza Salehi

Reputation: 930

Add images to linearlayout programmatically

I have a HorizontalScrollViewwith a LinearLayout in it and I want to add multiple images to layout programmatically with all of them in same height (about 300dp) and wrap content. something like they are fitted in the scroll area by scale. My problem is when the View is loaded there is only one image in scroll area and other images become visible by scrolling. Think like 4 images and there is only one visible with blank area around it (The first) but the other 3 are correctly added after each other. Here is my Code

            <HorizontalScrollView
                android:id="@+id/gallery_scroll"
                android:layout_width="fill_parent"
                android:layout_height="300dp" >

                <LinearLayout
                    android:id="@+id/gallery_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" >

                </LinearLayout>
            </HorizontalScrollView>

and the adding function :

for (final GalleryItem item : items) {
    ImageView iv = new ImageView(context);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT
                                            , LayoutParams.MATCH_PARENT);

     param.setMargins(0, 0, 10, 0);
     iv.setLayoutParams(param);
     iv.setImageBitmap(item);
     layout.add(iv);
}

the result : enter image description here

Upvotes: 0

Views: 2896

Answers (1)

GIGAMOLE
GIGAMOLE

Reputation: 1266

Use this:

RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

And try to set:

iv.setAdjustViewBounds(true);

Upvotes: 1

Related Questions