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
if changed the stretchMode="columnWidth"
and it is coming like this
- 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