Android Horizontal Vertical LinearLayout example tutorial

How to use Horizontal + Vertical Linear layout in android app.

Linear layout is one of the most usable layout for android application development because this layout gives application developer facility to manage text fields, widgets both directions horizontal and vertical because this layout comes with inbuilt attribute called as ” Orientation ” means app developer can decide that children tags appears vertically or horizontally. So here is the complete step by step tutorial for Android Horizontal Vertical LinearLayout example tutorial.

android-project-download-code-button

Note : The major difference between both Horizontal & Vertical linear layout is orientation. If you define orientation as horizontal then it automatically converts into horizontal format and if your will define orientation as vertical then it will become vertical layout.

Android Horizontal Vertical LinearLayout example tutorial.

Screenshot of Horizontal LinearLayout:

horizontal-linear-layout-android

Screenshot of Vertical LinearLayout:

Android Horizontal Vertical LinearLayout example tutorial

Note : code for both horizontal and vertical is same your just have to change Orientation in  ‘android:orientation=”horizontal” ‘ tag. vertical for android:orientation=”vertical” .

Code for MainActivity.java file.

package com.android_examples.com.linearlayoutandroid;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

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

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will
 // automatically handle clicks on the Home/Up button, so long
 // as you specify a parent activity in AndroidManifest.xml.
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
}

Code for activity_main.xml layout file.

 <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="com.android_examples.com.linearlayouthorizontal.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="1"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="2"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="3"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="4"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="5"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView6"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="6"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView7"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="7"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView8"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="8"
 android:layout_gravity="center"
 android:padding="10dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

 Click Here To Download Android Horizontal Vertical LinearLayout example tutorial project.

Leave a Reply

Your email address will not be published. Required fields are marked *