Beginner
    AJAX
    Android
    ASP.Net
    C#
    Cloud Computing
    Crystal Report
    Database
    Drupal
    HTML5
    JavaScript
    Joomla
    JQuery
    MVC
    php
    Reporting
    SharePoint
    SQL Server
    VB.Net
    Windows Phone
    WordPress
    WPF
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Creating an Android Application by using XML Layout




Creating an Android Application by using XML Layout - MindStick

Creating an Android Application by using XML Layout

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.

         Heres 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="http://schemas.android.com/apk/res/android" 

              android:id="@+id/textview01" 

              android:layout_width="wrap_content" 

              android:layout_height="wrap_content"

              android:textSize="30px"

              android:textStyle="bold"

              android:typeface="serif" 

              android:text="@string/hello"/>

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"?>

<resources>

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

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

</resources>

 

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

package com.example.firstApp;

 

import android.app.Activity;

import android.os.Bundle;

 

public class HelloAndroid extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

}

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.

Thanks.



 Author Information
Name: Rohit Kesharwani
Member Since: 8/8/2011
Article Submitted Date: 10/13/2011
Location:     India
Report Abuse Form
Reason:    
 
Total Online Users: 2756
Advertisement
MindStick SurveyManager
Advertise with Us
  
Dudelabs
Copyright © 2009 - 2013MindStick. All Rights Reserved.