Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a GridlView display the center of it on the screen?
    primarykey
    data
    text
    <p>I have a <code>GridView</code> like this(<code>main.xml</code>):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;/GridView&gt; </code></pre> <p>and the <code>GridView</code> item like this(<code>grid.xml</code>):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;ImageView android:id="@+id/grid_item_image" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginRight="10dp" android:src="@drawable/person" &gt; &lt;/ImageView&gt; &lt;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" &gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>As I have only 2 rows of items in the <code>GridView</code>, 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(<code>GridViewActivity.java</code>):</p> <pre><code>DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); width = metrics.widthPixels; height = metrics.heightPixels; </code></pre> <p>and in my adapter(<code>ImageAdapter.java</code>):</p> <pre><code>gridView = new View(context); gridView = inflater.inflate(R.layout.grid, null); gridView.setMinimumHeight(GridViewActivity.height/2); gridView.setMinimumWidth(GridViewActivity.width/2); </code></pre> <p>This doesn't give what I want: </p> <p><img src="https://i.stack.imgur.com/YLqZa.png" alt="enter image description here"></p> <p>you can see the 2nd row is not completely visible</p> <p><img src="https://i.stack.imgur.com/mTzHK.png" alt="enter image description here"></p> <p>Can anyone help me how to make these 2 rows fit exactly in the screen, or how to make the <code>GridView</code> display its center when the activity is called , just like this:</p> <p><img src="https://i.stack.imgur.com/Wtq4E.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload