HelloWorld Application in Android 4

16 July 2012 By Nithya Vasudevan 5,806 views One Comment
1 Flares Twitter 0 Facebook 0 Google+ 1 1 Flares ×

Project Description

  • In this Android tutorial we will see how to create and run a simple HelloWorld application.
  • Here, we use two different approaches to construct and display the UI components in Android Activity.

Environment Used

Setting up development environment

Before proceeding with this example, you need to setup the development environment with at least one Android platform in your SDK environment. To install Android SDK and configure ADT plugin for Eclipse read this page .

Create an Android Virtual Device (AVD)

You need Android Emulator to run and test your application. Before launching the emulator, you must create an Android Virtual Device (AVD) which defines device settings used by the emulator.
For creating an AVD, refer this link and return here to continue with this example.

Create Android Project

  • Open Eclipse
  • Select File -> New -> Project… or Right click on Package Explorer -> New -> Other…
  • Select “Android Project” found under the folder labeled “Android” and click Next.

  • Enter the project name as “HelloWorld” and click Next.

  • Select the platform version of SDK. For this application we are using Android API. Select this and click Next.

  • Enter Application Name as say, “Hello
    • This is the name that appears on the Android device. You can change this name later by modifying Hello element in strings.xml file located inside res/values folder.
  • Enter Package Name as com.theopentutorials.android
  • Enter Activity Name as HelloWorldActivity
    • An Activity represents single screen which creates a window in which you can place UI components. This is a subclass of android.app.Activity class.
  • Select the Minimum SDK version
    • The Minimim SDK Version should be the same as the Build Target that you chose in previous step.

  • Click Finish

Now, you will see an Android project (HelloWorld) in the “Package Explorer” view as shown below.


Open HelloWorld.java found inside HelloWorld -> src->com.theopentutorials.android and copy the code given below. This is an Activity class which is used to perform user actions. There can be many separate activities, but the user interacts with them one at a time. The Android system calls the onCreate() method when the Activity starts.

package com.theopentutorials.android;

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

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("Hello, Welcome to Android");
        setContentView(tv);
    }
}

Here, we create a TextView (text editor component in Android) object using its constructor which takes an Android Context instance as its parameter. Activity inherits from Context and HelloWorld class is a subclass of Activity, hence it is also a Context. So, we use “this” as Context reference to the TextView.
We pass the TextView instance to setContentView() to display it in Activity UI, otherwise a blank screen will be displayed.

Run the Application

  • Select Run -> Run.
  • Select “Android Application”.

The Eclipse ADT plugin automatically creates a new run configuration for this project and then launches the Android Emulator.

If you don’t find the above option then open Run -> Run Configurations… and follow the steps as shown in the screen.

Eclipse Run Last Launched option

Instead of repeating the above steps every time for running your Android application, you can optionally configure the launch operation to always launch as previously launched application by selecting Window -> Preferences. Preferences dialog window appears and do as shown below in the screen.

Output


Method 2: Using XML layout file


In the above code, we have programmed the UI layout. Alternatively, Android allows you to construct the UI layout using XML-based layout files which is the recommended option too.

The XML layout files are placed in res/layout/ folder of your project. The Eclipse ADT plugin automatically creates one layout file: main.xml. Steps below explain how to use XML layout files,

  • Open main.xml file located in res/layout folder (Once opened, it will be in graphical layout view, so you might need to click the “main.xml” tab at the bottom of the window to see the XML source).
  • Replace the content with the following XML
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/textview1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="@string/hello"/>
  • Each node is the name of a View or ViewGroup class
  • android:text is used to display the text which can be hard-coded using string value. But, here we have used hello string as defined in the res/values/strings.xml file.
  • Save the file.

Open res/values/strings.xml file to modify the hello string.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello, Welcome to Android</string>
    <string name="app_name">Hello</string>
</resources>

Now modify your HelloAndroid class to use the XML layout file.

package com.theopentutorials.android;

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

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

You can pass a View object to setContentView() or give it a reference to the layout resource. This resource is identified as R.layout.main, which is actually a compiled object representation of the layout defined in /res/layout/main.xml.

The Eclipse plugin automatically creates this reference for you inside the project’s gen/ [Generated Java Files] folder)/R.java class.

Now run your application and you will get the same output as before.

Tags: , , , , , ,

  • Pratibha Manisha Vyankatesh

    We have gone through the String concept.Very good explanation of topics with visuals and texts.Examples are very nicely explained.Thank you.