1

I can't understand the use of android:gravity and setGravity() of GridView.

android:gravity

Specifies the gravity within each cell.

center_horizontal - Place object in the horizontal center of its container, not changing its size.

setGravity(int gravity)

Set the gravity for this grid. Gravity describes how the child views are horizontally aligned. Defaults to Gravity.LEFT

gravity int: the gravity to apply to this grid's children

I am trying to center the last row as mentioned in this question, but I can't get it.

So, I tried to understand the use of android:gravity and setGravity().

I tried this code.

<GridView
     android:id="@+id/grid_view"
     android:stretchMode="none"
     android:numColumns="auto_fit"
     android:columnWidth="140dp"
     android:gravity="right"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />

its child view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/chapter_item_layout"
    android:minHeight="120dp"
    android:background="@color/red">

    <ImageView
        android:id="@+id/thumbnail_image"
        android:layout_width="68dp"
        android:layout_height="68dp"
        android:scaleType="fitXY"
        android:src="@drawable/icon" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:maxLines="2"
        android:ellipsize="end"
        android:id="@+id/title" />

</LinearLayout>

and it coming like this

stretchMode="none"

if changed the stretchMode="columnWidth" and it is coming like this

stretchMode="columnWidth"

  • The first image denoting that the child rows are not aligning right.
  • The second image denoting that the contents inside the child are not aligning right

Then what is the use of setting gravity?

I tried gridView.setGravity(Gravity.CENTER).

I need to display the dynamic items in the GridView.

How can I display the 3rd item in the center, if I have 3 items?

I tried setting layout_width="wrap_content" & putting my GridView inside

  • LinearLayout and setting layout_gravity:center
  • RelativeLayout and setting layout_centerInParent=true

Both cases not worked.

I cannot use TableLayout because I need to load more data on scroll to bottom, so I need to set OnScrollListener

Thanks in advance

Community
  • 1
  • 1
Vignesh Sundaramoorthy
  • 1,827
  • 1
  • 25
  • 38

1 Answers1

1

put your GridView inside a RelativeLayout and then set GridView's layout_centerInParent property to true.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
            android:id="@+id/grid_view"
            android:stretchMode="columnWidth"
            android:numColumns="2"
            android:columnWidth="140dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />

</RelativeLayout>

Because :

RelativeLayout gives you much better control over positions of children.

UpDate :

Try to change this two property

android:stretchMode="columnWidth"
android:numColumns="2"

Remove this property if you add in the GridView android:gravity="right".

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • No, it is not working because GridView taking its width as entire screen, So there is no use of making it center relative to its parent, & I tried now also as you said but it's not working – Vignesh Sundaramoorthy Dec 01 '16 at 12:56
  • @VigneshSundar Are you want your `GridView` in full Size or not as I am not understand what you want please post the screen shot what you want ? – Harshad Pansuriya Dec 01 '16 at 12:58
  • Please read the question carefully & fully, I am trying to center the last row as mentioned in [this question](http://stackoverflow.com/questions/19156385/how-to-cenralize-last-uneven-row-in-gridview-in-android), that question contains the clear images, as I already used 2 images, copy pasting images in that question will make my question bad – Vignesh Sundaramoorthy Dec 01 '16 at 13:09
  • @VigneshSundar Why only last row as per your ScreenShot you have move all of you row to center as I understand. Then why only last row. In your link the last row is not center but your question all row are not center and I am giving answer on that. – Harshad Pansuriya Dec 01 '16 at 13:12
  • First I started to center the last row. since I can't, I tried to understand the `setGravity` or `android:gravity=center`, but your answer did not center the entire GridView also if `android:layout_width="match_parent"` or `wrap_content` also – Vignesh Sundaramoorthy Dec 01 '16 at 13:19
  • @VigneshSundar both are same one is `xml way` and second is `programatical way`. – Harshad Pansuriya Dec 01 '16 at 13:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129540/discussion-between-vignesh-sundar-and-ironman). – Vignesh Sundaramoorthy Dec 01 '16 at 13:21