GridView shows only two columns
Asked Answered
V

2

7

I programmatically created a GridView of images to be used inside a Dialog. If I set the columns to autofit, I always get exactly two columns, whatever the size of the images is and whatever the screen size is. If I force the columns to a fixed number, then it works, but I cannot avoid the images to overlap on certain screen sizes, so an automatic management of the number of columns would be much better. Even more, when I try to set streching, it simply does not show anything!

My ImageAdapter:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

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

public int getCount() {
    return (IconC.Res.length);
}

public Object getItem(int position) {
    return (IconC.Res[position]);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(IconC.Res[position]);
    return (imageView);
}
 }

And the function that creates the Dialog (that is called elsewhere with a .show() )

    private void createPopUp() {
    d_icons = new Dialog(this);
    GridView gv_icons = new GridView(this);

//  gv_icons.setNumColumns(GridView.AUTO_FIT);  // autofit disabled, shows ONLY TWO COLUMNS
    gv_icons.setNumColumns(4); // instead, autofit forced, now it works, but I don't want it fixed!
    gv_icons.setGravity(Gravity.CENTER);
    gv_icons.setHorizontalSpacing(3);
    gv_icons.setVerticalSpacing(1);
//  gv_icons.setStretchMode(GridView.STRETCH_SPACING);  // stretching disabled, shows ZERO COLUMNS
    gv_icons.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    gv_icons.setAdapter(new ImageAdapter(this));
    gv_icons.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
            eventIcons.set(selectedItem, position);
            updateEventView();
            d_icons.dismiss();
        }
    });

    d_icons.setTitle(R.string.text_chooseicon);
    d_icons.addContentView(gv_icons, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    d_icons.setCancelable(true);
}

Can anyone spot why this happens? Thank you!!

Here's how it appears with the 4 fixed columns: i50.tinypic.com/2ebdfec.png

Here's how it is with the autofit: i50.tinypic.com/25jxo9s.png

Here is the layout with autofit on a horizontal tab: i49.tinypic.com/23r7qj5.png

Valance answered 14/1, 2013 at 12:33 Comment(0)
F
11

This post answers your question: Android: How does GridView auto_fit find the number of columns?

In order for the auto_fit to work you need to specify the column width: gridView.setColumnWidth( width );

Flashy answered 13/2, 2013 at 23:31 Comment(0)
R
0

Actually it depends on device size. As you first tested your application with making the no of columns fixed, grid displays only 2 columns because the space for the 3 image is not enough large so that it can fit in.

Respiration answered 14/1, 2013 at 12:37 Comment(5)
That is exactly my problem: 4 columns stay very well on my test screen, two create a lot of empty space between images. So it seems that the autofit does not work. Here's how it appears with the 4 fixed columns: i50.tinypic.com/2ebdfec.png Here's how it is with the autofit: i50.tinypic.com/25jxo9s.pngHeaven
autofit should work fine. because if you fix no of column, it will look very awkward on tabs. So go with autofit. try to resize your images. it will help you.Respiration
My problem is exactly what you suggest me: I want to use autofix, but although images are already small (already resized) it does not work. There must be some flaw in the code.Heaven
Here is the layout with autofit on a horizontal tab: i49.tinypic.com/23r7qj5.pngHeaven
what is the size of the image because as you did not set the image size so it is taking fill_parent by default. and why don't you try with xml, it will be a lot easier for you to work wit that.Respiration

© 2022 - 2024 — McMap. All rights reserved.