Pages Navigation Menu

Coding is much easier than you think

How To Create Relative Layout in an Android Application

How To Create Relative Layout in an Android Application

In this Tutorial, you will Learn How to create Relative Layout

Here you go!!!!

Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

Relative Layout: RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left of center). A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.

In RelativeLayout, you can use above, below, left and right” to arrange the component position. It’€™s the most flexible layout, that allow you to position your component to display in anywhere you want

The tutorial is about how to display an activity in relative layout.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to android application project, to create a new project in Eclipse refer Creation of Android Project.


Now let’€™s go to the coding section. This project requires following files.

Source Code:

  1. (Activity in relativelayout)

Activity Files:

  1. activity_relativelayout.xml – relativelayout activity

res – Values:

  1. strings.xml -€“ strings available in relative layout

Manifest file:

  1. AndroidManifest.xml

Here are the coding for the above files.

package com.example.relativelayout; // Relativelayout package
import android.os.Bundle; // A mapping from String values to various Parcelable types.
import; // Required to create an activity.
public class Relativelayout extends Activity { // all classes extends activity
	protected void onCreate(Bundle savedInstanceState) { // Create an activity/ screen
// displays activity in relative layout when app starts


<?xml version="1.0" encoding="utf-8"?>
<!-Setting the relative layout with horizontal orientation-->
<RelativeLayout xmlns:android=""
    android:layout_height="fill_parent" >
<!-- Button1 -->
<!-- Button2 -->
<!-- Use of rightof to place button2 to right of button 1 -->

  <!-- Button3 -->

<!-- Use of below to place button3 below button 2 -->

Menu : activity_relativelayout.xml

menu xmlns:android=""></menu>
Note: Since there are no menu settings involved in this project, delete the contents of the menu settings. This may cause errors in while building the project.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="8" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



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

<! -- Application Name1 -->

    <string name="app_name">Relativelayout</string>


<! -- specify properties such as height, padding, font color, font size, background color -->

        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    <style name="AppBaseTheme" parent="android:Theme.Light">
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->


Run the android application:

Android applications can be run on the android devices. You can either connect a hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project -> Build Project.

Build project

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched which displays the AVD on your screen.

You can see your app with the image set during the android project creation in AVD. Upon running the application, the emulator will be launched with the selected/ available AVD created on your screen.

Start Emulator

To test your application, unlock the screen and double click on your app.

You can see your app with the image set during the android project creation in AVD.

Emulator loading home page

AVD Home screen

To test your application, unlock the screen and double click on your app.

Relative layout

Thus the android application project is executed successfully.


Click Here To Share This..!!

%d bloggers like this: