how to set height of gridview programmatically android

33,078

Solution 1

Use this, as you want to retain the current layoutParams:

ViewGroup.LayoutParams layoutParams = yourGridview.getLayoutParams();
layoutParams.height = 50; //this is in pixels
yourGridview.setLayoutParams(layoutParams);

Edit: If you want to input the height as dp instead of pixels, translate the dp amount to pixels:

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

Solution 2

I think this should work:

GridView gridView = (GridView)findViewById(...);
ViewGroup.LayoutParams lp=new ViewGroup.LayoutParams(width,height);
gridView.setLayoutParams(lp);
Share:
33,078
Aswathy P Krishnan
Author by

Aswathy P Krishnan

Hi, I am a Mobile App Developer working mostly on Android Platform

Updated on May 10, 2020

Comments

  • Aswathy P Krishnan
    Aswathy P Krishnan about 4 years

    I want to set the height of my Gridview programmatically in my application.

    Is there any way to implement that?

    I just want to change the gridview height in two particularcases from the code.

    EDIT:

    <fragment
         android:id="@+id/mygridview"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_margin="10dip"
         class="com.myapp.android.MyGridViewClass" />
    

    MyGridViewClass extends Fragment where Gridview is populated.

     <GridView
        android:id="@+id/mygridview_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:columnWidth="90dp"
        android:gravity="center_vertical|center_horizontal"
        android:horizontalSpacing="1dp"
        android:numColumns="3"
        android:scrollbars="vertical"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="5dp" />
    

    In myGridViewClass onViewCreated, I am inflating the gridview using the code,

    gridView = (GridView)getView().findViewById( R.id.mygridview_container);
    gridAdapter = new MyCustomAdapter(context, list);
    gridView.setAdapter(gridAdapter);   
    

    Whenever the view is created I have to check the number of items in the gridview and set the height accordingly.

    When I tried setting the layoutParams() for gridview, I got this exception.

    android.view.InflateException: Binary XML file line #89: Error inflating class fragment
    

    Where line 89 corresponds to this line in the fragment class="com.myapp.android.MyGridViewClass"

  • ben75
    ben75 over 11 years
    double check the id your using in findViewById
  • Aswathy P Krishnan
    Aswathy P Krishnan over 11 years
    Thanks Friend, I tried this method too. Not working. I am using a fragment to which I am inflating this gridview. Maybe that is blocking this code. I am getting an InflateException for the class specified which is class="com.myapp.android.MyGridViewClass"
  • Aswathy P Krishnan
    Aswathy P Krishnan over 11 years
    I am inflating this GridView inside a fragment due to which I am getting an inflateException. :(
  • ben75
    ben75 over 11 years
    Provide more details please... Do you have a ClassCastException or an InflateException? Edit your question with your code and the trace if you need more help.