14
 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleBarBG"
        android:layout_alignParentLeft="true" >

    <RelativeLayout
        android:id="@+id/scrollContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >

    <GridView
        android:id="@+id/issueList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/archiveTitle"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/customshape"
        android:numColumns="3"
        android:overScrollMode="never"
        android:scrollbars="none" >
    </GridView>

</RelativeLayout>
 </ScrollView>

I would like to create a gridview that act like a table . For example, the size of the grid will increase that will make the gridview taller. Instead of hide the extra content ,I would like the grid view show all content and expand the height when there is additional content

How to implement this? thanks

4
  • Why don't you want the scrolling to be provided by the GridView? What features of scrolling does the ScrollView give you that the GridView doesn't?
    – Nilzor
    Dec 13, 2013 at 8:04
  • Thanks, the only problem is the height of gridview should be increase when the number of item in it is increased
    – user782104
    Dec 13, 2013 at 8:08
  • 2
    You might be looking for [ExpandableHeightGridView][1] [1]: stackoverflow.com/questions/8481844/gridview-height-gets-cut/…
    – Nilzor
    Dec 13, 2013 at 8:11
  • Is it easier to dynamic set the layout parms (height) of the grid view? thanks
    – user782104
    Dec 13, 2013 at 11:10

2 Answers 2

25
+25
public class MyGridView extends GridView {

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

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
3
  • 1
    You should explain a bit. Dec 20, 2013 at 19:32
  • Good work dude... Thanx for this code i try many this but i m not found anything at last i found this thank you again Jul 27, 2015 at 10:01
  • Thanks Dude.. saved time May 2, 2016 at 9:52
3

This is slightly cleaned up version of: Grid of images inside ScrollView

WrappedGridView.java:

/**
 * Use this class when you want a gridview that doesn't scroll and automatically
 * wraps to the height of its contents
 */
public class WrappedGridView extends GridView {
    public WrappedGridView(Context context) {
        super(context);
    }

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

Include in an XML layout like you would a GridLayout. Use an adapter to provide it views.

As far as I can tell, this is the simplest solution available now. There no other view available in the framework that handles wrapping. It would be nice if someone were to provide an elegant, automatically sizing table view. Modifying GridView.java for this purpose may not be a bad idea.

Alternatively, you may find one of the 'FlowLayout' projects acceptable. There is android-flowlayout and FlowLayout. These are a little more flexible than a simple grid and, I assume, a little less efficient. You also shouldn't need to provide them an adapter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.