0

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

Priya
  • 1,602
  • 4
  • 22
  • 37

2 Answers2

0

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

John Le
  • 1,116
  • 9
  • 12
0

try this. hope it will work

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="5"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:textAlignment="center"
    android:layout_centerInParent="true"        
    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>
NipunPerfect
  • 125
  • 1
  • 7