022 Java Android Program to Demonstrate Tip Calculator
Posted by Superadmin on December 21 2015 05:32:41
Java Android Program to Demonstrate Tip Calculator
Here is source code of the Program to Demonstrate Tip Calculator. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
The following code teaches u how to change the color of a component’s text and change the background color of the component.
Main Activity
package com.example.tip_calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText amt = (EditText) findViewById(R.id.bill_amt);
final EditText tip = (EditText) findViewById(R.id.bill_per);
final TextView result = (TextView) findViewById(R.id.res);
Button calc = (Button) findViewById(R.id.button1);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double amount = Double.parseDouble(amt.toString());
double tip_per = Double.parseDouble(tip.toString());
double tip_cal = (amount * tip_per) / 100;
result.setText("Result : " + Double.toString(tip_cal));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Actvity_Main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="47dp"
android:background="@android:color/holo_blue_dark"
android:text="Enter Your Bill amount"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/holo_red_dark" />
<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_marginBottom="14dp"
android:text="Result : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/res"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginBottom="34dp"
android:background="@android:color/black"
android:text="Calculate"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="96dp"
android:background="@android:color/holo_green_dark"
android:text="Enter Tip Percentage :"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/holo_red_dark" />
<EditText
android:id="@+id/bill_amt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="41dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/bill_per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="22dp"
android:ems="10" />
</RelativeLayout>