Center a LinearLayout and "percentage" margins

TotoTitus picture TotoTitus · Oct 8, 2012 · Viewed 10.4k times · Source

Edit: I have received already two comprehensive answers regarding fixed margins. While I've decided altogether to use fixed margins instead of weight margins, the original question remains open.

I am trying to obtain the following design in Android:

Desired Layout

A centered vertical list of stuff (TextViews, EditViews etc.) which leaves about 10% of the horizontal space free as left/right margin, with background.

What I tried and did not work/worked partially:

  • LinearLayout, vertical, as top-level layout. If the gravity is set to "centered", the background is limited to the size of the layout. Also, how does one set percentage margins (widths) in this manner?
  • LinearLayout on RelativeLayout: Background works, horizontal centering works, weights don't exist.
  • LinearLayout on LinearLayout: Background works, weights work, horizontal centering pushes all available space to the right.

(In the last two cases, my Eclipse also complains that one of the layouts is redundant.)

I have not posted code, having considered that this is somewhat more of a principle-related question. What would be the (best) way of accomplishing this?

Thank you.

XML corresponding to the last one of the test case:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:weightSum="1.0"
    android:background="#013c57" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Stuff -->
    </LinearLayout>
</LinearLayout>

Answer

Pratik picture Pratik · Oct 8, 2012

here is the simplest xml code for creating this type of layout check it

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#000"
    android:gravity="center">

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>
</LinearLayout>