A Basic Android Example
Android according to the definition provided at developer.android.com is
a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
It provides the necessary tools for developing software for the Android OS. Important parts are :-
1) SQLLite for Data Storage.
2) Optimized Graphics
3) Media support for common audio/video formats & still image formats.
4) GSM
5) Bluetooth
6) Camera, GPS , etc,
For developing Android applications we need to download the Android SDK, and also setup Eclipse on our computer. Both are available as free downloads. It is also possible to develop using only the Android SDK from the commandline.
Many setup guides are available on the internet, and setting up the environment should be a cinch.
A Sample Application
To begin, let us develop a simple application that shall read two numbers from two textfields and display their sum in the third.
First of all start Eclipse, New Project and select Android Project.
Since we are developing for the Samsung Galaxy, select Samsung Tab.
Android programming is done using the Java Programming Language, and user interface design is done using XML.
To create the user interface .
After creating the project.
Open main.xml
This will bring up the User Interface designer. It has two views. The Graphical Layout & the XML view
Now we shall drag and drop three EditText boxes and a Button onto the mobile.This is the user interface now.
This is the main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />
</LinearLayout>
We shall change the caption of the button to add.
This is done by adding a new String to
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Dedicated to Hypatia of Alexandria</string>
<string name="app_name">HypatiaBasicAndroid</string>
<string name="buttoncaption">Add</string>
</resources>
HypatiaBasicAndroidActivity.java
package hypatia.basic;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class HypatiaBasicAndroidActivity extends Activity {
/** Called when the activity is first created. */
EditText t1,t2,t3;
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
b.setOnClickListener(new ButtonHandler());
}
class ButtonHandler implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
int a=Integer.parseInt("" + t1.getText());
int b=Integer.parseInt("" + t2.getText());
int sum=a+b;
t3.setText("" + sum);
}
catch (Exception ex) {
// TODO: handle exception
t3.setText(ex.getMessage());
}
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class HypatiaBasicAndroidActivity extends Activity {
/** Called when the activity is first created. */
EditText t1,t2,t3;
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
b.setOnClickListener(new ButtonHandler());
}
class ButtonHandler implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
int a=Integer.parseInt("" + t1.getText());
int b=Integer.parseInt("" + t2.getText());
int sum=a+b;
t3.setText("" + sum);
}
catch (Exception ex) {
// TODO: handle exception
t3.setText(ex.getMessage());
}
}
}
}
In future posts, we shall cover all of android.
No comments:
Post a Comment