Thursday, 8 March 2012

Hello World - Your First Android Application "Create a New Android Project"

As you are done with the initial setup and the installation of the softwares to build your own android apps. now, we'll start building the basic and the first app.




Create a New Android Project

After you've created an AVD you can move to the next step and start a new Android project in Eclipse.
  1. In Eclipse, select File > New > Project.... If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain "Android Project". (After you create one or more Android projects, an entry for "Android XML File" will also be available.)
  2. Select "Android Project" and click Next.
  3. Fill in the project details with the following values:
    • Project name: HelloAndroid
    • Build Target: Select a platform version that is equal to or lower than the target you chose for your AVD.
    • Application name: Hello, Android
    • Package name: com.example.helloandroid (or your own private namespace)
    • Create Activity: HelloAndroid
    Click Finish.
    Here is a description of each field: 

    Project Name
    This is the Eclipse project name — the name of the directory that contains the project files.
     
    Application Name
    This is the human-readable title for your application — the name that appears on the Android device.
     
    Package Name
    This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity is generated. Your package name must be unique across all packages installed on the Android system; for this reason, it's important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.
    Create Activity
    This is the name for the class stub that is generated by the plugin. This is a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
     
    Other fields: The checkbox for "Use default location" allows you to change the location on disk where the project's files are generated and stored.
Your Android project is now ready. It should be visible in the Package Explorer on the left. Open the HelloAndroid.java file, located inside HelloAndroid > src > com.example.helloandroid). It should look this : i.e copy and past the below code in the HelloAndroid.java file.



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(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

now change the main.xml file to the below code as shown.

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout> 
 
 now you are good to run your first android application.
 the configuration to run your android application is given below.

The Eclipse plugin makes it easy to run your applications:
  1. Select Run > Run.
  2. Select "Android Application".
The Eclipse plugin automatically creates a new run configuration for your project and then launches the Android Emulator. Depending on your environment, the Android emulator might take several minutes to boot fully, so please be patient. When the emulator is booted, the Eclipse plugin installs your application and launches the default Activity. You should now see something like this:


 so, i guess you executed your first android application.

any further problems, pls write down your problems in the comments.
keep following for more android applications and solutions.

No comments:

Post a Comment