Tuesday, August 2

DEVELOP A SIMPLE ANDROID APPLICATION- Example with Code

Create a Simple Android  Application-
SIMPLE CONVERTER



"Simple converter" is the application intended to convert values from one unit to other that user can select. There are separate pages for Length, Temperature, weight.


Hi friends,

This time I would like to explain in detail how to make a simple Android application through an example - "SIMPLE CONVERTER". Here I provide the complete java code for this application. Step by Step explanation of the code is also given. "SIMPLE CONVERTER" is created in such a manner that many of the basic codes for android application development may  included. To understand them correctly, I tried to describe them stepwise.  Hope this will be useful for you.

This post includes:
  • How to make Android development platform? 
  • How to create User interface?
  • Code for basic Android application
  • Code for EditText
  • Code for Button
  • Code for Toast
  • Code for Spinner
Firstly, developing an simple Android application is  an easy job, if you know basics of java programming. Hope you are provided with eclipse to make a platform for android application development.

(I) Create application development platform

  1. To start application creation open eclipse and follow file--> New -->Project
  2. Select Android application from Android folder and "Finish".
  3. Enter necessary fields in dialog box.


 Look at the example..
  

                                  


(II) Create User Interface for Application

We need  two 'EditText 's  to enter values and two  'Button's for submitting our choices.We can include them in main.xml as follows.

Open res-->layout-->main.xml.

Edit main.xml it as:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SIMPLE CONVERTER"
android:gravity="center"
android:textSize="25sp"
/>

<EditText android:id="@+id/Value1"
android:layout_width="120sp"
android:layout_below="@+id/header"
android:hint="0.0"
android:layout_height="wrap_content">
</EditText>

<EditText
android:id="@+id/Value2"
android:layout_width="120sp"
android:hint="0.0"
android:layout_height="wrap_content"
android:layout_below="@+id/Value1">
</EditText>

<Button android:text="convert"
android:id="@+id/convert"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="@+id/Value2" >
</Button>

<Button android:text="Reset"
android:id="@+id/reset"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="@+id/convert">
</Button>

</RelativeLayout>

The output of the xml file will be as follows:







(III) Create Android Code for Application

Open the java file as src-->  <package name> -->    <filename.java>

In this example it is liengthConverter.java.
It will be like this:

package com.simpleconverter;
import android.app.Activity;
import android.os.Bundle;
public class lengthConverter extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


Edit the java code as:


package com.simpleconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class lengthConverter extends Activity {

private EditText v1,v2;
private Button Convert;
private Button Reset;
private double val1=0,val2=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

v1= ( EditText)findViewById(R.id.Value1);
v2= ( EditText)findViewById(R.id.Value2);
Convert = (Button)findViewById(R.id.convert);
Reset = (Button)findViewById(R.id.reset);
Convert.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
convertValues();
}
}
);

Reset.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
reset();
}
}
);
}

void convertValues()
{
if (v1.getText().length() > 0)
{
val1=Double.parseDouble(v1.getText().toString());
val2=InchToCm(val1);
}
else if(v2.getText().length() > 0)
{
val2=Double.parseDouble(v2.getText().toString());
val1=CmToInch(val2);
}
else
Toast.makeText(this,"please enter a value",Toast.LENGTH_LONG).show();
v1. setText(Double.toString(val1));
v2. setText(Double.toString(val2));

}
double InchToCm(double val)
{return(val*2.54);}
double CmToInch(double val)
{return(val/2.54);}


void reset()
{
v1.setText("");
v2.setText("");
}
}

This code converts centimeter value entered  in EditText _1 to Inch value and display it  in EditText _2.








Let us have  a detailed look on the code:

(IV) CODE EXPLANATION

I would like to describe each code segment separately , rather than explaining all in a while.

**How EditText works??

 Include this code in xml file. 

<EditText<Spinner
android:id="@+id/Value2"
android:layout_width="120sp"
android:hint="0.0"
android:layout_height="wrap_content"
android:layout_below="@+id/Value1">
</EditText>

Points to note:-
  • id   :: unique identifier for being accessed in java code.
  • hint:: hint displayed in EditText field.
  • text:: default text displayed in EditText field.


To access EditText in java, code as
v1= ( EditText)findViewById(R.id.Value1);

To check the no. of characters in text Field
int count=myEditText.getText().length();

To read a string
String val=myEditText.getText().toString();

To read a number
Double =val=Double.parseDouble(myEditText.getText().toString());


**How Button works??

To include a button ,code xml as:

<Button android:text="ButtonName"
 android:id="@+id/Button_id"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
>
</Button>

To access button in java code:
private MyButton = (Button)findViewById(R.id.ButtonNmae);

To recognize Button click,
MyButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
..........
}
}
);


**How Toast works??

Toast displays a tiny message for a short time and then disappears.

Code for Toast is
Toast.makeText(this,"my Toast...",Toast.LENGTH_SHORT).show();

parameter 1: context
parameter 2:Text to display.
parameter 2:duration of display; LENGTH_SHORT,LENGTH_LONG are available


(V)Modify the code

Current code has the only ability to convert from inch to centimeter and viceversa. We can improve it by including a Spinner with more unit conversion options.


** How to code Spinner??

Edit xml code as:

android:id="@+id/unit1"
android:gravity="center"
android:layout_height="wrap_content" android:layout_below="@+id/Value1" android:layout_width="fill_parent">
</Spinner>

To include options with spinner:

String[] items = new String[] {"inch<-->Centimeter",
 "Foot<-->Metre",
"Mile<-->Kilometre",
"Yard<-->Metre"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item, items);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

mySpinner.setAdapter(adapter);

To get the selection by Spinner, use the funtion:
int select=mySpinner.get SelectedItemPosition();

 We have now familiarized  with all necessary codings to deal with simple application, To improve functionality, edit  java code as,


void convertValues()
{
int u1= unit1.getSelectedItemPosition();
if (v1.getText().length() > 0)
{
val1=Double.parseDouble(v1.getText().toString());
val2=findValue1(val1,u1);
}
else if(v2.getText().length() > 0)
{
val2=Double.parseDouble(v2.getText().toString());
val1=findValue2(val2,u1);
}
else
Toast.makeText(this,"please enter a value",Toast.LENGTH_LONG).show();
v1. setText(Double.toString(val1));
v2. setText(Double.toString(val2));

}

double findValue1(double val,int unit)
{
double retval=0.0;
switch(unit)
{
case 0:retval= InchToCm(val);break;
case 1:retval= FootToMetre(val);break;
case 2:retval=MileToKm(val);break;
case 3:retval=YardToMetre(val);break;
}
return(retval);
}

double findValue2 (double val,int unit)
{
double retval=0.0;
switch(unit)
{
case 0:retval= CmToInch(val);break;
case 1:retval= MetreToFoot(val);break;
case 2:retval=KmToMile(val);break;
case 3:retval=MetreToYard(val);break;
}
return(retval);
}

double InchToCm(double val){return(val*2.54);}
double CmToInch(double val){return(val/2.54);}
double MetreToFoot (double val){return(val*3.28084);}
double FootToMetre (double val){return(val/3.28084);}
double MileToKm (double val){return(val*1.60934);}
double KmToMile (double val){return(val/1.60934);}
double MetreToYard(double val){return(val*1.09361);}
double YardToMetre(double val){return(val/1.09361);}


Now, our Simple converter is able to convert values on these units.


inch<-->Centimeter
Foot<-->Metre
Mile<-->Kilometre
Yard<-->Metre
   

Is Android programming funny now.??   :)
Your valuable suggestions are welcomed.!!
  



FOR MORE TRICKS ABOUT ANDROID PROGRAMMING WITH COMPLETE CODE AND EXPLANATION , CLICK HERE..!!!! :) :)



17 comments :

Alberto said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Nice tutorial, keep it up!

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Alberto

Thank you for your valuable feedback, Alberto..!! :)

Vj said... Best Blogger Tips [Reply to comment] Best Blogger Templates

nice 1 buddy :)

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@VjThanks Vj..!!!! :)

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Android app development
Nice to hear that.. Thank you..!! :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hi, Nice Tutorial...

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous
Thanks ..

madhu said... Best Blogger Tips [Reply to comment] Best Blogger Templates

AWESOME tutorial! I surfed through many sites for such a tutorial just explaining some basics and was dissatisfied with all until I reached this one. thanks! :)

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@madhu

wow!! Such a great compliment..!! Thank you very much..!! :-)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thank you! Would be even better if you used some syntax-highlighting stuff, much appreciated though.

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous

Thanks for your feedback.I just tried to include all essential stuff within one post. So described about an application.

Anyway, nice suggestion. Surely I'll keep it in mind :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

its very usefull...

dd said... Best Blogger Tips [Reply to comment] Best Blogger Templates

awesome job. its a complete package

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thank You friend..!! Hugs

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thank U dd..!! :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

very useful for beginners ..thanx..
i am new to android app development..i am not understanding the spinners implementation ....please help.....

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

I simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.

android development course fees in chennai | android app development training in chennai|Android Training institute in chennai with placement

Post a Comment