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.

Coding:

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

Source Code:

  1. Relativelayout.java (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.

Relativelayout.java:


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

activity_relativelayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-Setting the relative layout with horizontal orientation-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
<!-- Button1 -->
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Simple"/>
<!-- Button2 -->
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Codestuffs"
<!-- Use of rightof to place button2 to right of button 1 -->
        android:layout_toRightOf="@+id/button1"/>
 



  <!-- Button3 -->

     <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=".com"
<!-- Use of below to place button3 below button 2 -->
        android:layout_below="@+id/button2"/>
 
</RelativeLayout>

Menu : activity_relativelayout.xml

menu xmlns:android="http://schemas.android.com/apk/res/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 R.java while building the project.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.relativelayout"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.relativelayout.Relativelayout"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

strings.xml:

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

<! -- Application Name1 -->

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

Styles.xml:

<resources>
<! -- 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.
        -->
    </style>

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

</resources>

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..!!
Share

  • ramkumar

    Really appreciate you sharing this blog.Really looking forward to read more. Much obliged.

  • Tyler

    Great article post.Much thanks again. Want more.

  • Garrett Wisenor

    Keep working ,great job!

  • Olevia

    Very informative article.Really thank you! Much obliged.

  • meena

    Thanks on your marvelous posting!

  • Yuvan

    I couldn’€™t resist commenting. Perfectly written!

  • Asraff

    Im thankful for the article.Thanks Again. Keep writing.