title: How do I center a GridView inside a fragment with viewpager
body: <p>I have a Gridview inside a fragment of a ViewPager. I have 2x2 grid that I want to center on the parent, the ViewPager. It's <a href="http://i.stack.imgur.com/WqBHm.jpg" rel="nofollow">displaying like this</a>, only centering horizontally but not vertically My xml files are as follow.</p>
  
  <p>This is the activity_main_menu.xml</p>
  
  <pre><code>&lt;RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;ImageView
          android:id="@+id/background"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_below="@+id/actionBar"
          android:scaleType="centerCrop"
          android:background="@drawable/home_01" /&gt;
  
      &lt;android.support.v4.view.ViewPager 
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"
          android:layout_below="@+id/actionBar"
          android:layout_above="@+id/buttonBar"
          tools:context="com.excitingspace.areias_do_seixo.MainMenu" &gt;
      &lt;/android.support.v4.view.ViewPager&gt;
  
      &lt;com.viewpagerindicator.CirclePageIndicator
          android:id="@+id/titles"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="15dp"
          android:layout_above="@+id/buttonBar"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And this is the fragment_main_menu.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.excitingspace.areias_do_seixo.MainMenu$PlaceholderFragment" &gt;
  
      &lt;GridView
          android:id="@+id/gridView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentTop="true"
          android:columnWidth="150dp"
          android:gravity="center"
          android:listSelector="@android:color/transparent" 
          android:horizontalSpacing="10dp"
          android:layout_centerInParent="true"
          android:numColumns="2"
          android:verticalSpacing="10dp" &gt;
  
      &lt;/GridView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>EDIT, adds the ImageAdapter used to populate the GridView:</p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter {
  
  private static int PADDING = 0;
  private static int WIDTH = 0;
  private static int HEIGHT = 0;
  private Context mContext;
  private List&lt;Integer&gt; mThumbIds;
  private List&lt;Integer&gt; mThumbIds_highlighted;
  
  public ImageAdapter(Context c, List&lt;Integer&gt; ids, List&lt;Integer&gt; ids_highlighted) {
      mContext = c;
      this.mThumbIds = ids;
      this.mThumbIds_highlighted = ids_highlighted;
  
      int baseWidth = 150;
      int baseHeight = 150;
      int basePading = 20;
      float density = mContext.getResources().getDisplayMetrics().density;
  
      if ((mContext.getResources().getConfiguration().screenLayout &amp; Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
      {     
          baseWidth = 200;
          baseHeight = 200;
          basePading = 20;
      }
  
  
      ImageAdapter.WIDTH = (int) (baseWidth * density + 0.5f);
      ImageAdapter.HEIGHT = (int) (baseHeight * density + 0.5f);
      ImageAdapter.PADDING = (int) (basePading * density + 0.5f);
  }
  
  
  
  @Override
  public int getCount() {
      return mThumbIds.size();
  }
  
  @Override
  public Object getItem(int position) {
      return null;
  }
  
  // Will get called to provide the ID that
  // is passed to OnItemClickListener.onItemClick()
  @Override
  public long getItemId(int position) {
      return mThumbIds.get(position);
  }
  
  // create a new ImageView for each item referenced by the Adapter
  
  
  @SuppressWarnings("deprecation")
  @Override
  public View getView(final int position, View convertView, ViewGroup parent)
  {
  
      ImageView imageView = (ImageView) convertView;
      StateListDrawable sld = new StateListDrawable();
      Drawable draw1 = mContext.getResources().getDrawable(mThumbIds.get(position));
      Drawable draw2 = mContext.getResources().getDrawable(mThumbIds_highlighted.get(position));
  
      sld.addState(new int[] { android.R.attr.state_pressed }, draw2);
      sld.addState(StateSet.WILD_CARD, draw1);
  
      // if convertView's not recycled, initialize some attributes
      if (imageView == null) {
          imageView = new ImageView(mContext);    
          imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));
          imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
  //      imageView.setScaleX();
      }
  
      imageView.setBackgroundDrawable(sld);
  
  
      return imageView;
  }
  </code></pre>
  
  <p>}</p>