Friday, August 13, 2010

android os

ANDROID TUTORIAL

------------------------

Google has recently released the Android platform for developing Mobile applications. The language used for developing Android programs, is Java. But, it is not j2me.



No wireless application developer can ignore Android. Google is the best known brand name, among the users of the web and Android comes from Google..



I am presenting this hands-on tutorial, as a sequel to my j2me series. Adequate knowledge of core-java , especially, Event-handling , Swing and inner-classes is assumed. Though Android does not make use of Swing, it uses similar ideas.



We can develop Android lessons and applications in Eclipse environment. Google have provided an Eclipse-plugin for Android. This is the popular method. Google have not given direct support to Netbeans. But, some Netbeans users have developed a method for running Android in Netbeans .It is available at http://undroid.nolimit.cz/. You can find more screenshots and guidance in:

http://eppleton.com/blog/.



But, we can develop Android lessons without using either Eclipse or Netbeans. The necessary command-line tools have been provided by Google. I found that using these command-line tools is easier than either Eclipse or Netbeans method. So, I am basing all the following lessons on these tools. I think, most readers will agree with my view, after they try this method as well as Eclipse method. The Android site at 'code.google.com/android' has already given step-by-step instructions about Android in Eclipse. You can also get more details with screen shots from a wonderful website

at www.tanguay.info/web/welcome.php"

titled 'Edward's Web Developer site'. He gives excellent guidance with plenty of screen shots



But, the Android site lacks clarity, about the command-line method. Hence, I think I am adding something useful by writing on command-line method instead of simply repeating the material ,in Android site.



------------------------------------------

Let us begin from the beginning. The first step is downloading the Android SDK from

code.google.com/android/download

(version m5-rc14, Feb-12, 2008). Android was released in November, 2007 . It has been revised in the Feb-2008 version.Some of the earlier examples may not work in the newer version.

---

I am working in Windows-2000 and so I downloaded the windows version. The supported platform in Windows is either Windows-XP or Vista.( Mac OS 10 & Ubuntu Linux are the other platforms mentioned). However, it works well in my Win-2000. It is advisable to have atleast 512MB memory. The android SDK is a zip file. I unzipped it to C:\unzipped\android and later, I copied that folder to d:\android. If you want, you can simply copy it to another drive like g:\android also. In the following lessons

d:\android. is used.

If you want to develop using Eclipse, you must have installed either Eclipse3.2 or Eclipse3.3(Europa). I have tested with Eclipse3.2. No problem.It works. But, we require ADT (ie) Android Development Tools plugin for Eclipse, if you are using Eclipse.You can get this plugin from code.google.com/android/adt_download.You have to be careful about the ADT version number.It is ADT-0.3.3.

--

As my present focus is on command-line method, let me begin straight away and give a simple demo.

The procedure given here is common for all our experiments and so I will not be repeating it in each demo. So, please note it down carefully.

In my first demo, I will have a customary button and textbox ( called EditField in Android). When I click the button, I want the message "SUCCESS!" to be displayed in textbox. Just as an exercise, I am using two buttons and two textboxes.

-------------------------------------

The first step is to start the Emulator

cd to d:\android\tools

d:\android\tools>emulator

It will take a long time to get started. Do not be in a hurry. Wait till it gets fully started. And do not close that window carelessly by mistake. In that case, you will have to start it again and wait for a long time again. Finally, we get the emulator screen( starting)..





< xml="true" ns="urn:schemas-microsoft-com:vml" prefix="v" namespace="">

The second step is to give the following command., from another command window.

d:\android\tools>

activityCreator --out demo mypack.mydemos.demo

This means that my project is 'demo' and my package is 'mypack.mydemos'. Clear enough.

A number of folders are created automatically by this command. such as

tools\demo\src, tools\demo\bin ,

tools\demo\res.

We need to note the src and res folders carefully. We will place the java source file in src folder and main.xml file in res\layout, overwriting any files that are generated automatically. For the moment, we can think of the res\layout folder as the one which decides the gui design. As in asp.net, flex etc, the gui details are specified in xml file. But how shall we write the XML file? by hand? Not too difficult .But....Luckily, there is an open-source gui designer named 'DroidDraw' available in www.droiddraw.org .It is a nice tool and if you like it, you can send your appreciation to brendan.d.burns@gmail.com. He has given a simple tutorial too, on how to use this gui tool.

I downloaded this software from the above site. I unzipped it. ( any folder). When we click on the icon, we get the screen as given below.(droid-draw)











( drawing canvas area)





======================================







( toolbox & blank area)

====================================

Thus we get a window, showing the drawing canvas on leftside and toolbox and a blank area in the rightside. ( for printing purpose, I have split them into two screens) as above.

From the toolbox, we learn that we are having controls like button,check,radio,spinner,edittext(textbox) and textview (label) etc. There is also a combo to choose layout, in the canvas screen. . I am choosing 'absolute layout'. I simply drag and drop a button and an editview on the canvas.( drag and drop..do not click and drop! It won't work!).You will notice a number of tabs in toolbox. Select 'properties' tab.. After clicking the button on the canvas, give the id property of button as @id/button1. Similarly, for editview as @id/text1. Also, button2 and text2 After this, just click the 'generate' button at the bottom of the blank window. We get the following XML file(main.xml) automatically generated.

============================================

// main.xml















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








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

// d:\android\mydemos\ex4\demo.java ( checkbox)

(to be copied to tools\demo\mypack\mydemos)





package mypack.mydemos;





import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.*;

public class demo extends Activity

{

Button button1;

CheckBox check1,check2;

EditText text1;

@Override

public void onCreate(Bundle icicle)

{ super.onCreate(icicle);

setContentView(R.layout.main); text1=(EditText)this.findViewById(R.id.text1); check1=(CheckBox) findViewById(R.id.check1); check2=(CheckBox) findViewById(R.id.check2);

button1 =(Button) findViewById(R.id.button1);

button1.setOnClickListener(new clicker());

} class clicker implements Button.OnClickListener

{ public void onClick(View v)

{

String r = "";

if(check1.isChecked())

{

r = r+"java"+"\n";

}

if(check2.isChecked())

{

r = r+"c#";

}

text1.setText(r);

}

}



}

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



This is just the usual Java code and needs very little explanation. The only difference is the way , the controls are defined ( through res\layout\xml file).



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

DEMO-5 ( RADIO BUTTONS)



The next standard control is the radiobutton, within a radiogroup.

//d:\android\mydemos\ex5\main.xml

(to be placed in tools\demo\res\layout)













No comments:

Post a Comment