Linearlayout in Android example



           Android linear layout  is a common layout that arranges components in two orientations
  • Horizontal
  • Vertical
            Its very simple to code these layouts. You can see these layouts in various places in Android OS, 'Settings'  in android is a good example for Android linear layout arranged in Vertical orientation. The following example will clearly explain you the Android linear layout with horizontal and vertical orientations. You need not touch the Java side for this example, this can be done by coding in activity_main.xml and string.xml file.
Now lets go ahead with the example step by step..

Step 1 (Vertical Orientation)

             First place  linear layout in the application, this is oriented vertically.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
     >
</LinearLayout>

Step 2
             Lets place a TextView component inside linear layout.

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/wifi"
    android:textSize="15sp"    
    android:textColor="@android:color/white"
    android:background="@android:color/black"
    />

Text View has been set to have its font color as white and background as black.
android:text is assigned to wifi, which is declared inside res-values-string.xml file as follows.

<string name="wifi">Wi-Fi</string>

In the same way we can declare as many variables as required and place a TextView corresponding to these variables inside activity_main.xml. Since these TextView's are inside Vertical orientation we can see the text one below the another as shown in the screenshot. 

Step 3 (Horizontal orientation / listview)

           Now lets see how to show horizontal orientation inside the same example...
Basic layout will be something as shown below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
     >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/wifi"
    android:textSize="15sp"    
    android:textColor="@android:color/white"
    android:background="@android:color/black"
    />
.....
                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                             android:layout_width="fill_parent"
                             android:layout_height="fill_parent"
                             android:orientation="horizontal"
                             android:background="@drawable/bg"
                             >
                            //Horizontal orientation components goes here   

                         </LinearLayout>
</LinearLayout>

I have placed two buttons inside the horizontally oriented layout. Complete code for Android linearlayout is available below for download.

Download Source

Linear Layout Example

Kindly comment if you have any questions.

 



Reactions:

1 comment :