0

I am having trouble in getting the GridView content height programmatically since I am placing it inside a ScrollView, I just need to stretch the GridView height dynamically by getting its content height in PIXELS. Here's what I did:

@Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
     // TODO Auto-generated method stub 
     super.onWindowFocusChanged(hasFocus);
     System.out.println("...111Height..."+gridView.getMeasuredWidth());
}

but I want to obtain the value inside OnCreate so that I can feed it to layoutParams.height in:

GridView gridView = (GridView) findViewById(R.id.gridview_module);
    ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
    layoutParams.height = convertDpToPixels(iDontKnowWhatToPutHere, this);
    gridView.setLayoutParams(layoutParams);

and the method to convert from pixels to dp:

public static int convertDpToPixels(float dp, Context context) {
    Resources resources = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            resources.getDisplayMetrics());
}

EDIT:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="0"
    android:layout_weight="1"
    android:fillViewport="true"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <com.example.ExpandableHeightGridView
            android:id="@+id/gridview_module"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@id/tv"
            android:layout_weight="1"
            android:gravity="center"
            android:horizontalSpacing="20dp"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

        <Button
            android:id="@+id/dial_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/gridview_module"
            android:onClick="dialPhone"
            android:text="Dial Phone" />
    </LinearLayout>
</ScrollView>

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62

1 Answers1

0

To achieve this you need to create CustomGridview like this

public class CustomGridView extends GridView {

    private int old_count;
    private android.view.ViewGroup.LayoutParams params;
    private boolean isExpandFully = false;
    private int verticleSpacing = 4;
    private int noOfCollumns = 1;

    public int getNumColumns() {
        return noOfCollumns;
    }

    public void setNoOfCollumns(int noOfCollumns) {
        this.noOfCollumns = noOfCollumns;
    }

    public int getVerticleSpacing() {
        return verticleSpacing;
    }

    public void setVerticleSpacing(int verticleSpacing) {
        this.verticleSpacing = verticleSpacing;
    }

    public void setExpandFully(boolean isExpandFully) {
        this.isExpandFully = isExpandFully;
    }

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

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

    public CustomGridView(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            String namespace = "http://schemas.android.com/apk/res/android";
            noOfCollumns = attrs.getAttributeIntValue(namespace, "numColumns", 1);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

        setVerticalSpacing(getVerticleSpacing());
        if (isExpandFully) {
            try {
                if (getCount() != old_count) {
                    old_count = getCount();
                    params = getLayoutParams();

                    int len = (getCount() % getNumColumns() == 0 ? getCount() / getNumColumns() : getCount() / getNumColumns() + 1);
                    params.height = 0;
                    for (int i = 0; i < len; i++) {
                        params.height = params.height + (old_count > 0 ? getChildAt(0).getHeight() + getVerticleSpacing() : 0);
                    }
                    params.height += 10;
                    setLayoutParams(params);
                }
            } catch (Exception e) {
            }
        }
        super.onDraw(canvas);
    }

}

how to use

CustomGridView myGridview = (CustomGridView)findViewById(R.id.customGridview);

myGridview.setExpandFully(true);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Thank you for this but does this work if the GridView is inside a ScrollView? Because the main reason for this question is I have to expand to the content height so that the scrolling part will be taken by the ScrollView, and right now this doesn't work when I applied it. – Compaq LE2202x Sep 23 '13 at 07:32
  • yes this is working with me I have used this gridview in listview – Biraj Zalavadia Sep 23 '13 at 08:06
  • As I applied this, it seemed that the only shown in the GridView is the first row. The GridView itself is not expanded nor scrollable. Maybe the difference is that I placed my GridView inside ScrollView and not ListView? – Compaq LE2202x Sep 23 '13 at 12:03
  • how you placed it in xml. post you xml (layout file) – Biraj Zalavadia Sep 23 '13 at 12:06
  • Thank you! But it has the same outcome [here](http://stackoverflow.com/questions/8481844/gridview-height-gets-cut/8483078#8483078). The problem is that, I only have 2 rows of items, but the GridView is allocating 2 blank rows before my Button `android:id="@+id/dial_phone"` – Compaq LE2202x Sep 24 '13 at 02:21