Linear Layout android

Share this

In Linear layout elements are arranged linearly either

Horizontally or Vertically just by setting orientation tag to vertical or
horizontal.
android:orientation=“vertical”
or
android:orientation=“horizontal”
In above image on first screen elements are arranged
vertically
On second screen elements are arranged horizontally
You can put linear layout inside another layout
Nested Layout
In this image you can see element in red layout is
vertically arranged and element in blue layout is horizontally arranged.
Lets do it practically
1. Open android studio create new project name as Linear
Layout. I added package path
as com.androidcodr.linearlayout
2. open activity_main.xml
you can see hello world textview
change its text property to
Linear layout.
3. Add another textview below this.
LInear Layout in android
Set its text property to
These buttons are vertically arranged
4. To add new layout below textview just drag and drop a linear
layout (vertical)
LInear Layout in android
5. Set its property as shown in image.
<LinearLayout
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_below=“@+id/textView”
android:layout_alignParentLeft=“true”
android:layout_alignParentStart=“true”> </LinearLayout>
This is out outer layout which will contain other 2 layout.
6. Now add again vertical layout
LInear Layout in android
Set its property
Width match parent
Height wrap content
I have added layout margin to 5dp for all
7. Inside this layout add 3 button one by one as this is
vertical layout buttons will be added one below another.
See button property
Width match parent
Height wrap content.
Same margin 5dp to all.
Here is xml code for button
<Button
       android:text=“Button”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/button1”
android:layout_margin=“5dp” />
8. add another textview below this vertical layout.
Set its text property to
These buttons are horizontally arranged
9. Now we will add horizontal layout below textview we just
added.
Set its orientation to horizontal and other properties.
10. Again add 3 buttons here they will be added in horizontal
manner.
Set its property
Width wrap content

Height wrap content
Same margin 5dp to all.
11. So here is final screenshot of our screen.
12. Here is component tree of our elements on screen.
component Tree
13. See this how we can arrange items in horizontal and vertical
linear layout to show blog post title, date, author and post image.
14. Here is full xml code of activity_main.xml
<?xml version=“1.0”
encoding=“utf-8”?>
<RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/activity_main”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:paddingBottom=“@dimen/activity_vertical_margin”
android:paddingLeft=“@dimen/activity_horizontal_margin”
android:paddingRight=“@dimen/activity_horizontal_margin”
android:paddingTop=“@dimen/activity_vertical_margin”
tools:context=“com.androidcodr.linearlayout.MainActivity”>    <TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:id=“@+id/textView”
android:text=“Linear Layout” /><LinearLayout
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_below=“@+id/textView”
android:layout_alignParentLeft=“true”
android:layout_alignParentStart=“true”><TextView
android:text=“These Buttons are vertically arranged”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/textView2” /><LinearLayout
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_margin=“5dp”>

<Button
android:text=“Button”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/button2”
android:layout_margin=“5dp” />

<Button
android:text=“Button”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/button”
android:layout_margin=“5dp” />

<Button
android:text=“Button”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/button3”
android:layout_margin=“5dp” />
</LinearLayout>

<TextView
android:text=“These buttons are horizontally arranged”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/textView3” />

<LinearLayout
android:orientation=“horizontal”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_margin=“5dp”>

<Button
android:text=“Button”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:id=“@+id/button4”
android:layout_margin=“5dp” />

<Button
android:text=“Button”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:id=“@+id/butto5”
android:layout_margin=“5dp” />

<Button
android:text=“Button”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:id=“@+id/button6”
android:layout_margin=“5dp” />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

 

 

Comment below if you face any problem.

Share this

Leave a Reply

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