This is in the continuation of my previous post “
How To: Building Android Application with easy steps–Part 1”. In this tutorial, we will go over creating the sample application using the environment we have already setup.
Here is the details of the application we will build:
Number Counting: providing the buttons to increment/decrement the number present in the text box Step 1: create Android project
Select File → New → Other → Android → Android Project and create the Android project named
com.hashfold.android.numbercounting

click Next and select the Android SDK version on which your application will be build and compiled.
note: selecting the Android 2.1 is the best options as 90% of Android devices till date are based on this version.
Click next and make sure the information filled is matching with below:

and then click Finish
note: numbercountingActivity is the name of the activity which will get started when the application starts
here is the final project view after above steps:
Step 2: writing application code
so far we have completed creating the project space. Now its time to do some coding and creating the GUI components.
2.1 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"package="com.hashfold.android.numbercounting"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="com.hashfold.android.numbercountingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.2 strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="details">number counting application</string>
<string name="hello">Hello! number counting!</string>
<string name="app_name">NumberCounting</string>
<color name="bgcolor">#000000</color>
</resources>
2.3 main.xml layout – the GUI component
<?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" android:background="@color/bgcolor">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="+" android:id="@+id/Plus"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="@+id/Minus" android:text="-"></Button>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="@+id/Value" android:text="value"></TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Ok" android:id="@+id/Ok"></Button>
</LinearLayout>
</LinearLayout>
2.4 numbercountingActivity.java
package com.hashfold.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.hashfold.android.numbercounting.R;
public class numbercountingActivity extends Activity implements OnClickListener
{
Button Plus, Minus, Ok;
TextView Value;
int score = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Plus = (Button) findViewById(R.id.Plus);
Minus = (Button) findViewById(R.id.Minus);
Ok = (Button) findViewById(R.id.Ok);
Value = (TextView) findViewById(R.id.Value);
Plus.setOnClickListener(this);
Minus.setOnClickListener(this);
Ok.setOnClickListener(this);
}
public void onClick(View v)
{
boolean showText = false;
switch(v.getId())
{
case R.id.Plus: score++; showText = true; break;
case R.id.Minus: score--; showText = true; break;
case R.id.Ok: showText = true; break;
}
if(showText)
Value.setText(String.valueOf(score));
}
}
The
R.java is the resource file which is auto generated based on strings.xml and other related resource changes.
Step 3: running the application
setup the run configuration: click on Run->Run configurations

and then click on ‘New launch configuration’ and then fill in the information needed as below:


now click on Apply button and then Run button to run the application.
It will take a while in starting up the Android device emulator with the device select in above step “myDevice”.
Once the device is started, it will look like below:

now click on the plus “+” button and see how the application performs

If you wish you could start device in full screen mode by pressing Alt+Enter.

This helps in having full view of the application.
Having a different orientation view (click Ctrl+F11)

turn off the internet (click
F8)

toggle the internet (click
F8)