What’s in an android application?

Simplest android app

// HelloTheWorldActivity.java
package com.jamesfroggatt.HelloTheWorld;

import android.app.Activity;
import android.os.Bundle;

public class HelloTheWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
       <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="First android-puss application" />
       
        <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This button can be clicked" />

</LinearLayout>

Creates the following app:

An android program has several aspects to it which are detailed below from my Eclipse program.

Breaking this program down,

The main.xml file defines the user interface for the activity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
       <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="First android-puss application" />
       
        <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This button can be clicked" />

</LinearLayout>

In our main.xml, we show two TextViews, and one Button. The first text view references a string, meaning, the text that it shows will me the string ‘hello’ which is defined in the strings.xml file shown below.

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

    <string name="hello">Hello World, HelloTheWorldActivity!</string>
    <string name="app_name">HelloTheWorld</string>

</resources>

So we see that in the main.xml, the line

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

tells the program that we’re displaying a string (@string) called hello, and it looks for the string called hello in the strings.xml file as shown in the code above. The main.xml file also tells the program to display the text “First android-puss application”. This time, we are not referencing a string in strings.xml, we are explicitly stating the string in the main.xml file. Finally, we use the main.xml file to display a button on the screen.

Now taking a look at the AndroidManifest.xml file

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".HelloTheWorldActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

This file contains important information about the application. It defines the package name, version code and name. The app uses an icon

android:icon=”@drawable/ic_launcher”

called ic_launcher.png located in the drawable folder (this is the app’s icon). The file also contains the name of the program ‘app name’ which is a reference to the app_name variable in the strings.xml file.

android:label=”@string/app_name” >

Within the tags are also the activity tags. i.e., the activity that belongs to the application.

Check out the stuff about intents in the manifest file

  <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

The first line (MAIN) indicates this activity is an entry point for the application. The second line (LAUNCHER) tells the app that it can be launched from Launcher icon.

Leave a Reply