Android LinearLayout


Downlod LinearLayout Sample

Android LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.

All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child

Frequently used Xml attributes of Android linear layout

Xml attributes Description
android:id android:id uniquely identify the layout
android:orientation android:orientation specify the direction of arrangement of child View/ViewGroup and we will use "horizontal" for horizontal arrangement, "vertical" for vertical arrangements of child View/ViewGroup. The default is horizontal.
android:layout_weight With android:layout_weight we can specify a size ratio between multiple views. E.g. you have a MapView and a table which should show some additional information to the map. The map should use 3/4 of the screen and table should use 1/4 of the screen. Then we will assign the android:layout_weight of the map to 3 and the android:layout_weight of the table to 1.
android:weightSum android:weightSum is the weight sum of its all child View/ViewGroup.

Android LinearLayout file of above example is given below:


Login screen using Android LinearLayout

Now let's design a login screen using Android LinearLayout. Create a Android Studio project with name Login under a package xyz.apnatutorial.linearlayout with a empty screen.

Here is the content of modified files.

MainActivity.java

activity_main.xml

Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.