4

I am trying to display a GridView in a Dialog. Despite all my efforts, the GridView width grows to the entire screen, instead of wrapping to the columns. The layout and an image depicting the issue are below (I set a background color for the GridView to illustrate the issue).

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/colorgridview"
    android:background="#FF00CCBB"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:numColumns="4"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:columnWidth="70dp"
    android:stretchMode="none"
/>

enter image description here

ab11
  • 19,770
  • 42
  • 120
  • 207
  • Fixed layout_width does not work either. @ab11 - were you able to wrap the GridView? I'm trying to wrap it in a dialog. No luck so far. – FireFly Jun 24 '11 at 01:25
  • Have you tried ExpandableGridview in this topic : http://stackoverflow.com/questions/8481844/gridview-height-gets-cut – Phuc Tran May 29 '13 at 04:31

4 Answers4

1

I know that this post is a bit outdated. But if someone needs a solution to this, this answer may come in handy.

It is possible to set the view's width after screen measurement.

To do this:

Let your class implement the OnGlobalLayoutListener. The screen is measured when the onGlobalLayout method is called. We can do our magic here. GridView.getLayoutParams().width = ....

edit: I wasn't very clear on how to add the onGlobalLayoutListener. See plugmind's post, he shows how to add it. Can't figure out how to get view/layout width/height

Kind regards, Bram

Community
  • 1
  • 1
Bram
  • 4,533
  • 6
  • 29
  • 41
  • 1
    I ended up not using a GridView because of this issue, so I did not test this solution. But it sounds reasonable and you seem confident, so I will mark as answered so others can find this. Thanks! – ab11 Jan 20 '12 at 12:45
  • 1
    I did the same trick at my application, so I am confident that it works. Thanks for the accept :) – Bram Jan 20 '12 at 15:49
0

Had the same problem... I solved it with overridden onMeasure()

public class GridViewEx extends GridView {

    private int mRequestedNumColumns = 0;

    public GridViewEx(Context context) {
        super(context);
    }

    public GridViewEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GridViewEx(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setNumColumns(int numColumns) {
        super.setNumColumns(numColumns);

        if (numColumns != mRequestedNumColumns) {
            mRequestedNumColumns = numColumns;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (mRequestedNumColumns > 0) {
            int width = (mRequestedNumColumns * getColumnWidth())
                    + ((mRequestedNumColumns-1) * getHorizontalSpacing())
                    + getListPaddingLeft() + getListPaddingRight();

            setMeasuredDimension(width, getMeasuredHeight());    
        }
    }
}
Vitaliy Polchuk
  • 1,878
  • 19
  • 12
0

I think you should use android:layout_width="fill_parent" instead of android:layout_width="wrap_content" because wrap content use the minimum place it needs. On the other hand, fill_parent use all space needed. More over you should get rid of "android:columnWidth="70dp".

ClemM
  • 452
  • 3
  • 14
  • But, I want the columns to be of limited size and the dialog to wrap to that limited size. I do not want it to fill the whole screen. – ab11 Apr 13 '11 at 14:36
  • Oh my mistake sorry, i didn't understand that. Have you tried to build a custom Dialog with android:layout_width="wrap_content" ? I'm not sure but it could be the Dialog size which fill the screen. – ClemM Apr 13 '11 at 14:47
  • As noted in this link, I'm pretty sure Dialogs only "wrap_content": http://stackoverflow.com/questions/2961833/how-to-set-a-dialog-themed-activity-width-to-screen-width/2962168#2962168 – ab11 Apr 13 '11 at 15:37
0

It's certainly possible to set a fixed layout_width (in dp). Since the number of your columns is also fixed, could this be a workaround for you?

Matthew
  • 44,826
  • 10
  • 98
  • 87