I have a GridView like this(main.xml):
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:background="@color/black"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp" >
</GridView>
and the GridView item like this(grid.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:background="@color/white" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:src="@drawable/person" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/grid_item_image"
android:text="@+id/label"
android:layout_alignLeft="@id/grid_item_image"
android:layout_alignRight="@id/grid_item_image"
android:layout_marginTop="5dp"
android:textSize="15dp"
android:gravity="center"
android:textColor="@color/black" >
</TextView>
</RelativeLayout>
As I have only 2 rows of items in the GridView, I want the two rows to fill the screen exactly, not leaving any empty space or not going beyond the screen height, which needs a scroll to see fully. To do this I have done this(GridViewActivity.java):
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
and in my adapter(ImageAdapter.java):
gridView = new View(context);
gridView = inflater.inflate(R.layout.grid, null);
gridView.setMinimumHeight(GridViewActivity.height/2);
gridView.setMinimumWidth(GridViewActivity.width/2);
This doesn’t give what I want:

you can see the 2nd row is not completely visible

Can anyone help me how to make these 2 rows fit exactly in the screen, or how to make the GridView display its center when the activity is called , just like this:

First of all, if your
GridViewonly has 2 rows and 2 columns and you don’t plan to add more items, why don’t you use instead a simpleRelativeLayout(and aFrameLayoutfor each cell) to place those items in a grid like you want(or you could use aTableLayoutwith 2 rows)? It would be much easier to properly position them and the performance will be the same.If you still want to use the
GridViewthen you would have to find the dimensions of the screen(like you already did) substract from that the space between the rows, the title bar height and the notification bar height and then divide this to your 2 rows. Then you would use this size that you previously calculated to set it as the height for theLayoutParamsof the inflated item layout in thegetViewmethod of your adapter(don’t forget the width).My advice would be to use another layout then the
GridView. Example below(you could use anincludeto make it smaller)