0% found this document useful (0 votes)
44 views

Android Linear Layout

The document discusses Android LinearLayout, which is a view group that aligns child views vertically or horizontally. It has attributes like android:orientation to set the direction of arrangement, and android:gravity to control positioning of content. The example shows how to create a simple Android app with buttons in a linear layout, modifying the activity_main.xml layout file and MainActivity class.

Uploaded by

anandgyanbote
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Android Linear Layout

The document discusses Android LinearLayout, which is a view group that aligns child views vertically or horizontally. It has attributes like android:orientation to set the direction of arrangement, and android:gravity to control positioning of content. The example shows how to create a simple Android app with buttons in a linear layout, modifying the activity_main.xml layout file and MainActivity class.

Uploaded by

anandgyanbote
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

9/6/23, 7:15 AM Android Linear Layout

Android Linear Layout

Android LinearLayout is a view group that aligns all children in either vertically or horizontally.

Linear Layout

LinearLayout Attributes
Following are the important attributes specific to LinearLayout −

https://www.tutorialspoint.com/android/android_linear_layout.htm 1/5
9/6/23, 7:15 AM Android Linear Layout

Sr.No Attribute & Description

android:id
1
This is the ID which uniquely identifies the layout.

android:baselineAligned
2 This must be a boolean value, either "true" or "false" and prevents the layout from aligning its
children's baselines.

android:baselineAlignedChildIndex
3 When a linear layout is part of another layout that is baseline aligned, it can specify which of its
children to baseline align.

android:divider
4 This is drawable to use as a vertical divider between buttons. You use a color value, in the form of
"#rgb", "#argb", "#rrggbb", or "#aarrggbb".

android:gravity
5 This specifies how an object should position its content, on both the X and Y axes. Possible values
are top, bottom, left, right, center, center_vertical, center_horizontal etc.

android:orientation
6 This specifies the direction of arrangement and you will use "horizontal" for a row, "vertical" for a
column. The default is horizontal.

android:weightSum
7
Sum up of child weight

Example
This example will take you through simple steps to show how to create your own Android application using
Linear Layout. Follow the following steps to modify the Android application we created in Hello World
Example chapter −

https://www.tutorialspoint.com/android/android_linear_layout.htm 2/5
9/6/23, 7:15 AM Android Linear Layout

Step Description

1 You will use Android Studio to create an Android application and name it as Demo under a package
com.example.demo as explained in the Hello World Example chapter.

2 Modify the default content of res/layout/activity_main.xml file to include few buttons in linear layout.

3 No need to change string Constants.Android studio takes care of default strings

4 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.demo/MainActivity.java. This file can
include each of the fundamental lifecycle methods.

package com.example.demo;

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

public class MainActivity extends Activity {


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

Following will be the content of res/layout/activity_main.xml file −

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>

<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>

<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
https://www.tutorialspoint.com/android/android_linear_layout.htm 3/5
9/6/23, 7:15 AM Android Linear Layout

android:text="stop_service"/>

</LinearLayout>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>

Let's try to run our modified Hello World! application we just modified. I assume you had created your AVD
while doing environment setup. To run the app from Android studio, open one of your project's activity files
and click Run icon from the toolbar. Android studio installs the app on your AVD and starts it and if

everything is fine with your setup and application, it will display following Emulator window −

Now let's change the orientation of Layout as android:orientation="horizontal" and try to run the same
application, it will give following screen −

https://www.tutorialspoint.com/android/android_linear_layout.htm 4/5
9/6/23, 7:15 AM Android Linear Layout

 Print Page

https://www.tutorialspoint.com/android/android_linear_layout.htm 5/5

You might also like