Sunday, December 12, 2010

Sample Android application - Tip calculator


For this application, we will first generate the User Interface and then we will write code for the functionality of the application. The User Interface is created using the XML file.

First create a new project with name 'Tip Calculator'. Name the package as com.Android. Name the java file as tipcalc.java

The default XML file generated in your project would contain the following code.

Old main.xml file content:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, tipcalc"
/>
</LinearLayout>


Now replace this XML code with the following code.

New main.xml file content:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, tipcalc"
/>
<TextView
android:id="@+id/widget28"
android:layout_width="99px"
android:layout_height="17px"
android:text="Amount of Bill"
android:layout_x="40px"
android:layout_y="32px"
>
</TextView>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage to Tip"
android:layout_x="40px"
android:layout_y="82px"
>
</TextView>
<TextView
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of People"
android:layout_x="40px"
android:layout_y="132px"
>
</TextView>
<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="18px"
android:text="Tip Amout"
android:layout_x="40px"
android:layout_y="262px"
>
</TextView>
<TextView
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="18px"
android:text="Total Per Person"
android:layout_x="40px"
android:layout_y="352px"
>
</TextView>
<TextView
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total To Pay"
android:layout_x="40px"
android:layout_y="302px"
>
</TextView>
<Button
android:id="@+id/btncalculate"
android:layout_width="87px"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_x="40px"
android:layout_y="182px"
>
</Button>
<Button
android:id="@+id/btnreset"
android:layout_width="86px"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_x="140px"
android:layout_y="182px"
>
</Button>
<EditText
android:id="@+id/txtbillamount"
android:layout_width="wrap_content"
android:layout_height="35px"
android:text="100"
android:textSize="18sp"
android:layout_x="200px"
android:layout_y="22px"
>
</EditText>
<EditText
android:id="@+id/txtpercentage"
android:layout_width="51px"
android:layout_height="36px"
android:text="10"
android:textSize="18sp"
android:layout_x="200px"
android:layout_y="72px"
>
</EditText>
<EditText
android:id="@+id/txtpeople"
android:layout_width="wrap_content"
android:layout_height="39px"
android:text="1"
android:textSize="18sp"
android:layout_x="200px"
android:layout_y="122px"
>
</EditText>
<TextView
android:id="@+id/txttipamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_x="200px"
android:layout_y="262px"
>
</TextView>
<TextView
android:id="@+id/txttotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_x="200px"
android:layout_y="302px"
>
</TextView>
<TextView
android:id="@+id/txtperperson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_x="200px"
android:layout_y="352px"
>
</TextView>
</AbsoluteLayout>

The code you have done till now will generate the User Interface for the application. Now we shall write the code to implement the functionality of the project.

Your tipcalc.java file would contain the following code.

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


Replace the above code with the following code:

New tipcalc.java content:
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class tipcalc extends Activity
{
private EditText txtbillamount;
private EditText txtpeople;
private EditText txtpercentage;
private TextView txtperperson;
private TextView txttipamount;
private TextView txttotal;
private Button btncalculate;
private Button btnreset;
private double billamount = 0;
private double percentage = 0;
private double numofpeople=0;
private double tipamount = 0;
private double totaltopay = 0;
private double perperson = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
txtbillamount = (EditText)findViewById(R.id.txtbillamount);
txtpeople = (EditText)findViewById(R.id.txtpeople);
txtpercentage = (EditText)findViewById(R.id.txtpercentage);
txtperperson=(TextView)findViewById(R.id.txtperperson);
txttipamount=(TextView)findViewById(R.id.txttipamount);
txttotal=(TextView)findViewById(R.id.txttotal);
btncalculate = (Button)findViewById(R.id.btncalculate);
btnreset = (Button)findViewById(R.id.btnreset);
btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }});
}
private void calculate()
{
billamount=Double.parseDouble(txtbillamount.getText().toString());
percentage=Double.parseDouble(txtpercentage.getText().toString());
numofpeople=Double.parseDouble(txtpeople.getText().toString());
tipamount=(billamount*percentage)/100;
totaltopay=billamount+tipamount;
perperson=totaltopay/numofpeople;
txttipamount.setText(Double.toString(tipamount));
txttotal.setText(Double.toString(totaltopay));
txtperperson.setText(Double.toString(perperson));
}
private void reset()
{
txtbillamount.setText("");
txtpeople.setText("");
txtpercentage.setText("");
txtperperson.setText("");
txttipamount.setText("");
txttotal.setText("");
}
}

Now launch the application on your Android emulator and you will see the application on your emulator. Click on it to see the functionality of your application.
If you are able to see the calculated tip amount for the input details provided by you, then your application is working well. Congratulations! 

No comments:

Post a Comment