Android provides two types of UI construction model i.e. programmatic UI layout and XML-based layout. In this article I am going to explain how to create an Android application in Android Emulator by using XML layout files.

Expand the Project in the Package Explorer à Expand res folder à Layout folder à Select main.xml.

· Here’s an XML layout file (main.xml) that is identical in behavior to the programmatically constructed file:

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

<TextView xmlns:android=""

The general structure of an Android XML layout file is a tree of XML elements, wherein each node is the name of a View class (this example, however, is just one View element). You can use the name of any class that extends View as an element in your XML layouts, including custom View classes you define in your own code. This structure makes it easy to quickly build up UIs, using a more simple structure and syntax than you would use in a programmatic layout. This model is inspired by the web development model, wherein you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data.
In the above example there is only one View Element (TextView) which has eight attributes. 

·         Inside the res/values/ folder open strings.xml, modify the strings.xml as shown below:

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

     <string name="hello">Welcome! This is my first Android Application</string>
     <string name="app_name">FirstAndroidApp</string>


·         Now open and modify your HelloAndroid class and use the XML layout. Edit the file as shown below:

package com.example.firstApp;

import android.os.Bundle;

public class HelloAndroid extends Activity {
     /** Called when the activity is first created. */
     public void onCreate(Bundle savedInstanceState) {

After creating the above changes run your application:

Right click on the project (FirstAndroidApp) which is shown in the Package Explorer à Select Run As à Android Application.

Output as shown below:

Creating an Android Application by using XML Layout

After performing the above steps you can easily create and run an Android application by using XML layout files.


  Modified On Mar-23-2018 02:09:02 AM

Leave Comment