How-To: A Guide To Creating An Android App

Android App Developemt.

Assuming you now have all the software in place and you’ve set up a virtual device in the Android SDK and AVD manager, you can create a new project. In Eclipse IDE choose ‘File > New > Project’. In the New Project wizard, select the ‘Android’ folder and choose ‘Android project’. Click ‘Next’. You now have a new window for your project details.

To start with, we’ll set up a simple ‘Hello world’ application that just displays some text when launched. In the field marked ‘Project name’, enter HelloAndroid. For ‘Application name’ enter Hello, Android. For ‘Package name’ supply com.example.helloandroid and for ‘CreateActivity’, enter HelloAndroid. Click ‘Finish’. These parameters are used to set up your project in Eclipse.

The project name is also the name for the directory in your workspace that will contain your project files. Eclipse will create it for you. Assuming you accepted the default Windows workspace of C:Users[username]workspace, you’ll find the above directory at C:Users[username]workspaceHelloAndroid.

If you browse to this in Windows Explorer, you’ll see a number of subfolders and files set up as part of the project. The application name is the title of your app, which will be displayed in the Android device. Change this to change the name of the app. You need to be a bit more careful with the package name. This is the namespace for the package where your source code resides. It needs to follow the rules for naming packages in Java.

It also needs to be unique across the Android system, which is why a domain style package is used; ‘com.example’ is reserved for examples like this. If you develop an app that’s published, you’ll need to use your own namespace. This usually relates to the organisation publishing the app.

‘Create activity’ relates to the class stub generated by the plug-in. An activity is basically an action. It might need to set up a user interface if it needs one. We left other project fields at their default values, but it’s useful to know what they do. ‘Min SDK version’ lets you set the minimum API required by your application.

If ‘Use default location’ is ticked, your project will be saved in your workspace. You can opt to change this if you want to store the files elsewhere. ‘Build target’ is the platform target for your application. It’s the minimum version of Android that it will run on.

If you develop an app to run on an earlier version of Android, it should run on a later one too, but one developed for a later version of the platform probably won’t run on an earlier version. For an example like this, the build target isn’t critical as long as you can get your application to run in the emulator. It’s more of a concern when you come to release an app.

Finally, the option to create the project from an existing example enables you to select some existing code to modify. You’ll find this of more interest as you move on to greater programming challenges.

Modify the Code

You should now see your project displayed in the Package Explorer, which is shown in the left-hand pane of Eclipse. Double-click ‘HelloAndroid’ to expand it. Also expand ‘src’ and ‘com.example.helloandroid’. Double-click ‘HelloAndroid.java’ to see the code that’s already been set up. In the main pane you should see the following text:

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

If you can’t see all of this, try looking to the left-hand side of the pane and expanding any plus signs that indicate collapsed code. This defines your application without actually doing anything at this stage. To make it do some work, we need to add an object that will contain your text.

Having done that, we also need to specify the text. Below ‘import android. os.Bundle;’ add the following line:

import android.widget.TextView;

Also add the following above the two sets of closing curly brackets:

TextView tv = new TextView(this);
tv.setText(“My First Android App”); setContentView(tv);

You can replace the text within the quotes to make your app say whatever you like. Check that the code in its entirety reads as the following, assuming you kept the displayed text the same:

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText(“My First Android App”);
setContentView(tv);
}
}

Save the changes to your code. You can now try it out in the Android emulator. In Eclipse, choose ‘Run > Run > Android application’. The emulator launches. It can take a few minutes to boot into Android, so be patient. Once booted, your app should run automatically and you’ll see a grey title bar with the app name in it. Below this, your chosen text is displayed.

Press the ‘Home’ button in the emulator to return to the Android home screen. Click the ‘Applications’ button to see the list of available applications. Among these you should see ‘Hello, Android’. Select this to launch your app again.

 

Comments are closed.