Reinherd
Reinherd

Reputation: 5506

Filling a layout of X images

I'm trying to fill a LinearLayout / Relative, whatever, with many images. Uncontable.

This is my implementation:

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/header"
        android:layout_centerHorizontal="true" >

        <LinearLayout
            android:id="@+id/marcasImages"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
</ScrollView>

So I add a same image multiple times:

for (int i=0; i<10; i++){
    ImageView imatgeExemple = new ImageView(this.getApplicationContext());
    imatgeExemple.setImageResource(R.drawable.imageexample);
    contenidorImatgesGaleria.addView(imatgeExemple);
}

contenidorImatgesGaleria is marcasImages.

But what I'm getting as a result is a single row of X images (X = as many images as it can fill by its resolution). So for example, if I do that loop for 1k times, only 4 images are shown.

I'd like to have as many rows as needed.

This is what's happening:

enter image description here

And this is what I'm trying to achieve:

enter image description here

I've tried to use GridLayout but nothing changed.

<GridLayout
   android:id="@+id/marcasImages"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >
</GridLayout>

Any idea of which layout should I use?

Upvotes: 0

Views: 131

Answers (1)

Tiago Almeida
Tiago Almeida

Reputation: 14237

You can use a GridView to achieve what you are trying to do.

GridView is a lot like a ListView. You have an adapter (that can be a costum one) and you set your adapter.

Therefore, you need to:

Add a GridView to your layout

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>

Define a costum adapter that holds images

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        // if it's not recycled, initialize some attributes
        if (convertView == null) {  
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7 };
}

Set a that costum adapter that holds images in your activity that has your layout

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

Note that I resumed and copy pasted from the link I provided. You have more info there.

Upvotes: 1

Related Questions