Search This Blog

27 August 2011

Android First Program Extended







I think you had completed the installation of the android SDK in Eclipse environment. Now we move towards the first program. This is not ordinary HELLO WORLD program, but it is somewhat about an extended version of the “hello world”.

For that follow the following step to make a successful android application.





Step 1:

Start the eclipse; it is considered that you have installed an android SDK to eclipse. If not please see my blog on “Android Installation for Developers”.

 





 

·         Now go to file-> new.

·         And you see there are so many applications are there.

·         Choose “android project”. If you don’t find the android project then go to the “other” and the other window opened. In that window find “android” and select “android project”.

·         Now give the name to the project, like “MyFirst”.

·         Select the version of the android, it is that on which type of devices you are targeting for.

·         Then give package name, it is recommended that the package name should start with the “src”, for example “src.androidexample”.

And it will create some files. Which file contains what that I will tell you in my next blog.

 


 

Step 2:

Now go to quick look on which file you get. (in order to up to down)

·         The first file is of source. In that file the java class and java file are stored for your android application logic.

·         The second file is gen file. It contains the generated code of the android project. That will be done automatically by the eclipse.

·         The android 2.3.3 this file contains the jar file of the android that will help you to make android application.

·         The res file, it contain the resources of the layout and the values that are necessary for the GUI of an android application. The both the files are in XML formats.

 

Step 3:

Now first we make the android GUI. In that you just simply drag and drop the component in to the layout.

For that go to the layouts and in that file double click on main.xml,

Then it will open a layout for the android application.

 


 

Now drag and drop the button control in to the screen. And save it.

Step 3:

Now go to the “src” folder and click on it, then in that folder open the “myFirstActivity.java”

In that write the code like I had mention.


*************************************************************************************

package src.first;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyFirstActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button= (Button)findViewById(R.id.btnClick);
        button.setOnClickListener(new View.OnClickListener() {
                                               
                                                public void onClick(View v) {
                                                                // TODO Auto-generated method stub
                                                Toast.makeText(MyFirstActivity.this,"Hello World", Toast.LENGTH_LONG).show();               
                                                }
                                });
    }

}

*************************************************************************************


Step 4:

If you have not started your android AVD then it is recommended that start your AVD and then run your project.

If you don’t know how to start an android AVD then See my Blog “Android Installation for Developers”.

 

Step 5:

Right click on the Myfirst file and run as android application. And then the android project starts.

In that project when you click on the button it will show you the message (called toast) “Hello World”, and it is the Extended Hello World application in Android.

It look like this

 


 

 


Making an android application looks easy but i will show you how to make complex application in android in my next blogs.

Thanks for reading my blog. If you have any query please mail me and tell me the problem, I will try to resolve your problem.