GridView is not behaving like it is supposed to. This screenshot shows that the GridView (in landscape mode) is flushed left. I want it centered.
This is the XML layout for the GridView.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/templatelandscape"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<GridView
android:id="@+id/commandsbarlandscape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:padding="0dp"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:numColumns="auto_fit"
android:columnWidth="52dp"
android:stretchMode="spacingWidth"
android:gravity="fill_horizontal" />
</LinearLayout>
What am I doing wrong ?
Android LinearLayout – Center Align To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.
Click on the gridView->properties->RowStyle:horizontal-align:Center // what ever you want.
Try layout_gravity and gravity attributes in the LinearLayout to make all the childs to be centered by default. This above is a splash screen activity. So there are two childs - ImageView and TextView. Both are center aligned.
The default orientation is horizontal.
So in this post, we will show you how you can center any view in Linear and Relative layouts. For adjusting any view to center in LinearLayout, you will need to modify your layout file with parameter android:layout_gravity=”center”
To center make gravity = center like wise. Then apply that layout as the layout for your grid view. Ex : Sample layout name : myCellitemlayout.xml <TextView> android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" </TextView> Now add this to your gridView adapter.
Make sure that you haven't set any gravity to parent layout, and then set layout_gravity="center" to your view or layout. I experienced this while working with nested RelativeLayouts.
so you're designing the Linear Layout to place all its contents (TextView and Button) in its center and then the TextView and Button are placed relative to the center of the Linear Layout Try <TableRow android:gravity="center_horizontal"> This will center the inner LinearLayout within the tablerow.
android:layout_height="wrap_content"
has no meaning for widgets that have their own scrolling, like GridView
.
android:layout_alignParentTop="true"
and android:layout_centerInParent="true"
are for RelativeLayout
, not LinearLayout
.
Get rid of your android:layout_weight="1.0"
, change your android:layout_height
to "fill_parent"
or a specific height, change the LinearLayout
to a RelativeLayout
, and you may be in better shape.
I don't know if you resolved your problem (i hope yes, and also i'm very late on answering on this post!), but here is a clue:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<GridView
android:id="@+id/gridview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidth" >
</GridView>
</LinearLayout>
Unfortunately this work only if there is 1 content on the grid. Maybe someone can improve it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With