Thursday, July 3, 2014

Android Linear Layout Example | Linear Layout demo in Android | How to make linear layout in android

Android Linear layout:   Basically android have 5 types of layouts, we will discuss them one by one with examples-




Here I am Introducing to Linear Layout:
As its name Linear means in line. Like if you want thing to display one by one in line you should use LinearLayout. Linearlayout have 2 types of orientation horizontal and vertical. Let us see them with examples-

       1)      Vertically
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button2" />
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button3" />
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button4" />
   
   
</LinearLayout>

      2) Horizontally
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button2" />
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button3" />
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button4" />
   
   
</LinearLayout>

And use this MainActivity class to display your views-

package com.androidhub4you.linaerlayout;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
       }

}



Your comments and feedback are awaited!
Thank You!



No comments:

Post a Comment