[RSS]

Introducing Android Development with NetBeans

Updated on 26 November, 2008 for NetBeans 6.5 and Android SDK 1.0 Release 1

Contributed by: Amit Kumar Saha


In this article, I shall show you how you can start with developing for the Android platform using NetBeans using nbandroid.

Prerequisites

Before you can get started with developing Android applications, you are advised to acquaint yourself with:

  • The Android Framework
  • Overview of writing applications for Android

Please refer to the Android SDK documentation for information on the above.

You will need:


Installing the Android plugins for NetBeans

The nbandroid project provides the plugins for Android development on NetBeans. There are 3 possible ways to install the plugin:

  • PREFERRED Using nbandroid update center
  • Download and install NBMs. Those are available for download from here. Once downloaded, install all the plugins by going to Tools > Plugins > Downloaded.
  • You could also clone the project's mercurial repository and build the module suite yourself and then install them. (How?)


Adding the Android platform

After you have installed the plugins,you have to add your Android platform by going to Tools->Java Platforms:

android-plat.png

Point it to your Android SDK location:

android-plat-1.png


Creating a new Android project

Now, go to File->New Projects. You should now see a new entry for Android:

new-project.png

Go ahead with the default application name (or change it!)

new-project-1.png

You should see a new project structure as below:

project-structure.png

The file MainActivity.java is as shown below:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.hellodroid;

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

/**
 *
 * @author amit
 */
public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here        
    }

}

As described in the Android SDK documentation, an Android application is organized as a set of activities. The file MainActivity.java that has been generated is the main activity class for this application.


Running your project

Now, build and Run your project as you would do with any other NetBeans project. Your project builds successfully and the Android emulator boots up:

run-default.png

Where is our Hello World?

Just add

setContentView(R.layout.main);

to the above file in place of

// ToDo add your GUI initialization code here

This should have been generated by the plugin, because the main.xml containing the main view was generated.

Alternatively we can do the following.

We shall now add support to the skeleton code to display the text- Hello, Android. Add the following to the above file in place of the "// ToDo add your GUI initialization code here":

TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);

You will notice that the IDE complains- "Cannot find symbol". This is because we need to import the class TextView. Its easy with NetBeans. Just click on the yellow bulb and select "Add import for.."

import-tip.png

The file should now look like:

package org.me.androidapplication1;

import android.app.Activity;

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


/**
 *
 * @author amit
 */
public class MainActivity extends Activity {

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

}

Now, run the project again

run.png


Conclusion

The 'nbandroid' project is just getting started with plenty of opportunities to contribute. Join in at http://kenai.com/projects/nbandroid/

References

  1. Google Android Documentation

Attachments

add-android-platform-1.png Info on add-android-platform-1.png 19023 bytes
add-android-platform.png Info on add-android-platform.png 59010 bytes
add-new-class.png Info on add-new-class.png 17971 bytes
add-samples.png Info on add-samples.png 22843 bytes
android-plat-1.png Info on android-plat-1.png 23439 bytes
android-plat.png Info on android-plat.png 54400 bytes
clone-repo.png Info on clone-repo.png 34556 bytes
clone-repo1.png Info on clone-repo1.png 36307 bytes
default-run.png Info on default-run.png 59004 bytes
import-tip.png Info on import-tip.png 33624 bytes
install-plugin.png Info on install-plugin.png 48739 bytes
nbandroid-nb.png Info on nbandroid-nb.png 5979 bytes
nbandroid.png Info on nbandroid.png 8671 bytes
new-project-1.png Info on new-project-1.png 40279 bytes
new-project.png Info on new-project.png 41680 bytes
open-nb.png Info on open-nb.png 16225 bytes
project-structure.png Info on project-structure.png 19264 bytes
run-default.png Info on run-default.png 59004 bytes
run.png Info on run.png 60619 bytes
samples.png Info on samples.png 46928 bytes