How to Make a Simple Android Application- Example with java code


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


Hi friends,

This time I would like to say about how to make a simple Android Application with an example - "SIMPLE CONVERTER". Here I provide complete java code for the application. Step by Step explanation of the code is also given. "SIMPLE CONVERTER" is created in such a manner that many necessary code for android development may get included. To understand them correctly, I've described them stepwise. Hope this will be useful for you.

This blog 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
You might be also interested in Installing Android and Working Tips :Read it Here
For improving the speed of Application Read it Here
How To Get the Gps location  in Android Phone : Code Snippet Here
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..
    
    Creating Development platform

(II) Create User Interface for Application

We need two 'EditText 's to enter values and two 'Button's .We include them in main.xml as follows.

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

Edit main.xml it as:-


http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


android:gravity="center"
android:textSize="25sp"
/>


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



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







The output of the xml file will be as follows:




(III) Create Android Code for Application

Open the java file as src--> -->

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.




Sample output


Now take a detailed look on code:

(IV) CODE EXPLANATION

I would like to describe each code segment segmentwise , rather than all explained at once.

**How EditText works??

Include this code in xml file.


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


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"
>


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 little message for a short time and 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 functionality by including a Spinner.



** 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">


To include options with spinner:

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

ArrayAdapter adapter = new ArrayAdapter

(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 familarized with all necessary codings to deal with simple application, To improve funtionality, 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 converter values


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


Is the Android progamming not much difficult now..?? :)

Comments

  1. This is one of the amazing and good post.Your blog is doing brilliant work.This is one of the quality post.
    Android app developers

    ReplyDelete
  2. Hey, I had a doubt I'd be grateful if you could help. How did you get this : android.R.layout.simple_spinner_item, which you pass in the adapter??
    I'm totally stuck on this. Kindly help.
    Thank you.

    ReplyDelete
  3. Interesting Tutorial!!
    Heres another tutorial to make a simple app for your android device on your android device itself!!!
    http://farasbee.com/blog/2012/03/07/developing-a-android-app-without-a-laptop-or-a-computer/

    ReplyDelete
  4. Can you upload the source code?thanks in advance

    ReplyDelete
  5. please upload the physics calculator code...your code is amazing...please send to this email jon_elena0330@yahoo.com

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. the code explanation is amazing.

    ReplyDelete
  8. Thanks so very much for taking your time to create this very useful and informative site. I have learned a lot from your site. Thanks!!


    JAVA Training Institutes in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

How To Install LEX and YACC in Linux or Ubuntu

Lex Program To Check The Syntax of For Loop

Listing/Delisting of an article in to an Assortment in SAP SCM Retail