Ankita Deshmukh
Ankita Deshmukh

Reputation: 237

How to assign wrap_content as height to dynamically loaded gridview

I am loading gridview dynamically with buttons. So for that I am using scrollview, But if i assign wrap_content as height to gridview all the buttons are not displayed. I dont want to assign any static height for the gridview. This is the code which I am using:

<ScrollView
                 xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" >

              <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  android:paddingLeft="5dip"
                  android:paddingRight="5dip" >

                  <GridView
                      android:id="@+id/gridviewtable"
                      android:layout_width="fill_parent"
                      android:layout_height=wrap_content"
                      android:horizontalSpacing="10dp"
                      android:numColumns="4"
                      android:verticalSpacing="10dp" >
                  </GridView>

              </LinearLayout>
          </ScrollView>

Upvotes: 5

Views: 10402

Answers (5)

Axel L&#243;pez
Axel L&#243;pez

Reputation: 276

You need to create a new class for example

public class WrappingGridView extends GridView {

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

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

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec = heightMeasureSpec;
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
        // The great Android "hackatlon", the love, the magic.
        // The two leftmost bits in the height measure spec have
        // a special meaning, hence we can't use them to describe height.
        heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    }
    super.onMeasure(widthMeasureSpec, heightSpec);
}

}

also you need to change in your XML

<com.your.project.WrappingGridView
                android:id="@+id/gridviewtable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:horizontalSpacing="10dp"
                android:numColumns="4"
                android:verticalSpacing="10dp"/>

and finally in your java class you need to chance the object GridView to WrappingGridView

Upvotes: 11

Luke Allison
Luke Allison

Reputation: 3218

This is a Frankenstein of many answers before me but it is the only thing has worked for me so far:

private static void resizeGridView(GridView gridView, int rows) {
    int measuredHeight = gridView.getMeasuredHeight();
    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    params.height = measuredHeight * rows;
    gridView.setLayoutParams(params);
    gridView.requestLayout();
}

Call this after you've updated the content to be displayed to the gridView.

Upvotes: -1

kukku
kukku

Reputation: 489

try this

int totalHeight = (count / <number of items in a row>);

    if (count % <number of items in a row> != 0) {
        totalHeight++;
    }

    ViewGroup.LayoutParams params = gridview.getLayoutParams();

    params.height = <height of a row> * totalHeight;

    gridview.setLayoutParams(params);
    gridview.requestLayout();

Upvotes: 2

A. Binzxxxxxx
A. Binzxxxxxx

Reputation: 2871

This or something similar should do it. (code as i remember it) myImageView.setLayoutParams(new ImageView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, (LinearLayout.LayoutParams.WRAP_CONTENT));

Upvotes: -1

FD_
FD_

Reputation: 12919

android:layout_height="wrap_content" cannot be used for subclasses of AdapterView (e.g. ListView and GridView). The GridView would have to get each row's height in order to calculate its own height.

Remove the ScrollView and the LinearLayout, you don't need them. The GridView already has its own scrolling logic built-in.

Upvotes: -1

Related Questions