Android LinearLayout

In this Android tip, I am going to show you how to use LinearLayout that is typically used in Android applications. The LinearLayout allows you to group and arrange widgets in a single row or column. You can specify row alignment or column alignment by using the android:orientation attribute. This attribute can be set in the <LinearLayout> tag. By supplying the vertical value to the attribute, the widgets are aligned in a single column. In contrast, if this attribute has horizontal value, the widgets are aligned in a single row. Another commonly used attribute is android:layout_weight. The android:layout_weight tells each widget in the LinearLayout to occupy the remaining space. The larger value the more space is occupied. This attribute is set in an individual widget. The default value of the android:layout_weight is zero.



In the example activity_main.xml file below, there are two LinearLayouts. The outer layout arranges its inner LinearLayout and one Button in a single column. The inner LinearLayut has zero weight and the weight of the button is 2 so the button expands vertically to occupy the remaining space. The inner LinearLayout arranges two Buttons in a single row. The two Buttons has the equal layout weight so they expand to occupy the remaining space equally.

<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: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=".MainActivity"
   android:orientation="vertical"
   >
<LinearLayout
android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   >
   <Button
     android:id="@+id/bt1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="button1"
     android:layout_weight="1"
      />
   <Button
     android:id="@+id/bt2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="button2"
     android:layout_weight="1"
      />
   </LinearLayout>
   <Button
     android:id="@+id/bt3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="button3"
     android:layout_weight="2"
     />

</LinearLayout>

Android-LinearLayout

Posted by: Dara | post date: 08-15-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.