Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a GridView in its LinearLayout parent?

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 ?

like image 858
Jacques René Mesrine Avatar asked Oct 09 '09 15:10

Jacques René Mesrine


People also ask

How do you center something in LinearLayout?

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.

How do I center something in GridView?

Click on the gridView->properties->RowStyle:horizontal-align:Center // what ever you want.

How do you center an image in LinearLayout?

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.

What is the default orientation of a LinearLayout?

The default orientation is horizontal.

How to center any view in linearlayout in Android?

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”

How to center a text view in a grid view?

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.

How do I Center a view in a relative layout?

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.

How to center a linear layout within a tablerow?

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.


2 Answers

  1. android:layout_height="wrap_content" has no meaning for widgets that have their own scrolling, like GridView.

  2. 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.

like image 42
CommonsWare Avatar answered Sep 29 '22 09:09

CommonsWare


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!

like image 52
JJ86 Avatar answered Sep 29 '22 07:09

JJ86