Center layout Android
Asked Answered
N

2

8

How can I center these buttons on Android? alt text

The code I'm using in the layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

      <TextView android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           android:text="@string/hello" />

      <LinearLayout android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="horizontal">

           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
      </LinearLayout>
</LinearLayout>
Nehemiah answered 25/11, 2010 at 17:6 Comment(0)
G
15

Make the LinearLayout containing the buttons to wrap_content on width and to have center_horizontal on gravity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

 <TextView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" />

 <LinearLayout android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
      android:layout_gravity="center_horizontal">
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
       </LinearLayout>
</LinearLayout>
Gumboil answered 25/11, 2010 at 17:55 Comment(0)
H
1

I'd like to know if anyone else has any comments on this as well. I've always done it with a TableLayout and set both stretchColumns and collapseColumns on all columns so it scales on all screen sizes. A little like this:

 <TableLayout
  android:layout_width="fill_parent"
  android:layout_height="47dp"
  android:stretchColumns="*"
  android:collapseColumns="*"
  android:padding="0px"
  android:background="@drawable/bottom_bg">
  <TableRow>
    <ImageView
        android:id="@+id/article_button_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_home_selector"
        android:layout_centerHorizontal="true" />
   <ImageView
        android:id="@+id/article_button_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_sharearticle_selector"
        android:layout_centerHorizontal="true" />
    <ImageView
        android:id="@+id/article_button_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_settings_selector" 
        android:layout_centerHorizontal="true"/>
    <ImageView
        android:id="@+id/article_button_about"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true"      
        android:src="@drawable/button_about_selector"/>
</TableRow>
</TableLayout>

I do wonder if it is the best way though.

Humanity answered 25/11, 2010 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.