Priya Priya - 3 years ago 183
Android Question

GridView Item in Center Android

I am trying to set my gridview item in center. You can see in below image that first column have low margin then last column. I want make it in center so it can look good.
enter image description here

My XML of GirdView is like below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:background="?attr/colorPrimaryDark"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:textColor="?attr/TextColor"
android:layout_width="wrap_content"
android:layout_height="@dimen/toolbar"
android:text="Go to Page:"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="@dimen/appName"
android:id="@+id/title"
android:padding="8dp" />

<GridView
android:layout_gravity="center"
android:background="?attr/colorPrimary"
android:id="@+id/grid_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
android:layout_below="@+id/title"
android:layout_marginTop="7dp"
android:layout_above="@+id/dialog_dismiss"
/>

<TextView
android:id="@+id/dialog_dismiss"
android:layout_width="wrap_content"
android:layout_height="@dimen/toolbar"
android:text="Dismiss"
android:textStyle="bold"
android:textColor="?attr/TextColor"
android:padding="8dp"
android:textSize="@dimen/appName"
android:gravity="center"
android:layout_marginEnd="@dimen/lblTime"
android:layout_marginRight="@dimen/lblTime"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>

</RelativeLayout>


and Item XML is like below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
style="@style/SelectableItemBackground"
android:layout_height="wrap_content">

<TextView
android:textStyle="bold"
android:textAppearance="@style/TextAppearance.AppCompat.Title.Inverse"
android:textColor="?attr/TextColor"
android:textSize="@dimen/appName"
android:gravity="center"
android:id="@+id/textview_dialogue"
android:layout_width="@dimen/nav_but_size"
android:layout_height="@dimen/nav_but_size"
android:visibility="visible" />
</RelativeLayout>


Let me know if someone can help me for come out from issue. Thanks

Answer Source

I found an existing answer here. Hope it helps.

So the first thing you should enable "Show layout bound"(see here) so that you are able to see the detail of your layout as the image below. It helps you to correct you layout easily:

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download