GridView shows only two columns

I programmatically created a gridview of images to be used inside the dialog box. If I set the columns to autofit, I always get exactly two columns, regardless of image size and any screen size. If I force the columns to a fixed number, then it works, but I can not avoid the coincidence of images at certain screen sizes, so automatic control of the number of columns would be much better. Moreover, when I try to set the stretch, it just shows nothing!

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 (which is called elsewhere with .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 understand why this is happening? Thanks!

Here's what it looks like with 4 fixed columns: i50.tinypic.com/2ebdfec.png

Here's how to do this with auto-detection: i50.tinypic.com/25jxo9s.png

Here is a layout with auto-detection on a horizontal tab: i49.tinypic.com/23r7qj5.png

+7
source share
2 answers

This entry answers your question: Android: how will GridView auto_fit find the number of columns?

For the auto_fit function to work, you need to specify the column width: gridView.setColumnWidth (width);

+9
source

Actually, it depends on the size of the device. When you first tested your application, indicating that the number of columns has not been set, the grid displays only 2 columns, because the space for 3 images is not large enough to fit.

0
source

All Articles