Tuesday, December 30, 2008

LinearLayout in Android (Vertical, XML)

Reading this, you probably have a clue that LinearLayout lays out object linearly and can only have a vertical and horizontal layout, no diagonal layout.

Here is an example of a vertical LinearLayout:
(update: horizontal LinearLayout here)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
>
    <Button android:id="@+id/RecordBtn"
        android:text="Record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button android:id="@+id/PlayBtn"
        android:text="Play"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_width="fill_parent" />

    <Button android:id="@+id/StopBtn"
        android:text="Stop"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="left" />

    <Button android:id="@+id/PauseBtn"
        android:text="Pause"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
</LinearLayout>

Quick Explanation:
There would be 4 buttons here, Record, Play, Stop and Pause, each one have its id and a wrap_content height, 3 of the buttons have fill_parent which mean fill up all spaces their width, gravity is text-align in css. See the picture below to better understand.

Further reading
LinearLayout in Hello View in Android

Update History
   Somewhere before 2012: Here is how you can set this as your layout in your java code

   Jan 17, 2012 - Visual Update
Post a Comment