Android Tutorial -Coding a basic application

Android Tutorial -Coding a basic application

This section provides a whirlwind tour of building an Android application. The example application is about as simple as you can imagine: a modified “Hello Android” application. You’ll add a minor modification to make the screen background color all white so you can use the phone as a flashlight. Not very original, but it will be useful as an example. Download the full source code.

To create an application in Eclipse, select File > New > Android project, which starts the New Android Project wizard.
Figure 5. New Android project wizard
The new Android project wizard

Next, you create a simple application with a single activity, along with a UI layout stored in main.xml. The layout contains a text element you’re going to modify to say Android FlashLight. The simple layout is shown below.
Listing 1. Flashlight layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/all_white">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" android:textColor="@color/all_black" 
   android:gravity="center_horizontal"/>
</LinearLayout>

Create a couple of color resources in strings.xml.
Listing 2. Color in strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Android FlashLight</string>
    <string name="app_name">FlashLight</string>
    <color name="all_white">#FFFFFF</color>
    <color name="all_black">#000000</color>
</resources>

The main screen layout has a background color defined as

all_white

. In the strings.xml file, you see that

all_white

is defined as an RGB triplet value of #FFFFFF, or all white.

The layout contains a single

TextView

, which is really just a piece of static text; it is not editable. The text is set to be black and is centered horizontally with the

gravity

attribute.

The application has a Java source file called FlashLight.java, as shown below.
Listing 3. Flashlight.java

package com.msi.flashlight;
import android.app.Activity;
import android.os.Bundle;
public class FlashLight extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

The code is boiler-plate directly from the New Project wizard:

This article of Android Tutorial to Learn Android Development has been

take from IBM please click the link to read the entire article

learn android programming

This entry was posted in Android Tutorials and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
Security Code: