title: How do I calculate GridView content height in pixels?
body: <p>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 <strong>PIXELS</strong>. Here's what I did:</p>
  
  <pre><code>@Override 
  public void onWindowFocusChanged(boolean hasFocus) 
  { 
       // TODO Auto-generated method stub 
       super.onWindowFocusChanged(hasFocus);
       System.out.println("...111Height..."+gridView.getMeasuredWidth());
  }
  </code></pre>
  
  <p>but I want to obtain the value inside <code>OnCreate</code> so that I can feed it to <code>layoutParams.height</code> in:</p>
  
  <pre><code>GridView gridView = (GridView) findViewById(R.id.gridview_module);
      ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
      layoutParams.height = convertDpToPixels(iDontKnowWhatToPutHere, this);
      gridView.setLayoutParams(layoutParams);
  </code></pre>
  
  <p>and the method to convert from pixels to dp:</p>
  
  <pre><code>public static int convertDpToPixels(float dp, Context context) {
      Resources resources = context.getResources();
      return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
              resources.getDisplayMetrics());
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>&lt;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" &gt;
  
  &lt;ScrollView
      android:id="@+id/scrollview"
      android:layout_width="match_parent"
      android:layout_height="0"
      android:layout_weight="1"
      android:fillViewport="true"
      android:scrollbars="none" &gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" &gt;
  
          &lt;TextView
              android:id="@+id/tv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/hello_world" /&gt;
  
          &lt;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" /&gt;
  
          &lt;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" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p></p>