0

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?

Reinherd
  • 5,476
  • 7
  • 51
  • 88
  • 1
    you tried gridview. put that code here. – Dhaval Parmar Apr 04 '13 at 09:11
  • @DhavalSodhaParmar gridLayout, not gridview. – Reinherd Apr 04 '13 at 09:16
  • 1
    you want to show images. then use gridview why use this type of stuff. because you have to do everything from coding like fix row and columns. what ever for gridlayout check: http://android-developers.blogspot.in/2011/11/new-layout-widgets-space-and-gridlayout.html and http://stackoverflow.com/q/11863329/1168654 and – Dhaval Parmar Apr 04 '13 at 09:23

1 Answers1

1

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.

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • No problem. I belive you will find everything there. The hardest part if to know what kind of control it is needed. I just pasted because the page can be moved or something. – Tiago Almeida Apr 04 '13 at 09:28
  • Ok, implemented it and it's working. Exactly what I needed. It's hard for a begginer to know which control use at early stages of learning hah. – Reinherd Apr 04 '13 at 09:34