title: how to set height of gridview programmatically android
body: <p>I want to set the height of my <strong>Gridview</strong> programmatically in my application. </p>
  
  <p>Is there  any way to implement  that? </p>
  
  <p>I just want to change the gridview height in two particularcases from the code.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>&lt;fragment
       android:id="@+id/mygridview"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_margin="10dip"
       class="com.myapp.android.MyGridViewClass" /&gt;
  </code></pre>
  
  <p>MyGridViewClass extends Fragment where Gridview is populated.</p>
  
  <pre><code> &lt;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" /&gt;
  </code></pre>
  
  <p>In myGridViewClass <code>onViewCreated</code>, I am inflating the gridview using the code,</p>
  
  <pre><code>gridView = (GridView)getView().findViewById( R.id.mygridview_container);
  gridAdapter = new MyCustomAdapter(context, list);
  gridView.setAdapter(gridAdapter);   
  </code></pre>
  
  <p>Whenever the view is created I have to check the number of items in the gridview and set the height accordingly.</p>
  
  <p>When I tried setting the layoutParams() for gridview, I got this exception.</p>
  
  <pre><code>android.view.InflateException: Binary XML file line #89: Error inflating class fragment
  </code></pre>
  
  <p>Where line 89 corresponds to this line in the fragment
      class="com.myapp.android.MyGridViewClass" </p>