From this tutorial you will learn how to develop a Simple Calculator.This Calculator takes two numbers as input and performs Addition ,Subtraction , multiplication and Division, and the result is displayed. In this tutorial we are using-
3 EditText , 2 for collecting input and the other to display output.
2 Textviews- To locate the EditText.
4 Buttons -Addition ,Subtraction , multiplication and Division.
Here is the full code for the program,
Application Name : SimpleCalculator
Project Name : SimpleCalculator
Activity Name : MainActivity.java
Layout Name : activity_main.xml
3 EditText , 2 for collecting input and the other to display output.
2 Textviews- To locate the EditText.
4 Buttons -Addition ,Subtraction , multiplication and Division.
Here is the full code for the program,
Application Name : SimpleCalculator
Project Name : SimpleCalculator
Activity Name : MainActivity.java
Layout Name : activity_main.xml
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Enter First No : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:text="Enter Second No : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/textView2"
android:layout_marginTop="45dp"
android:ems="10" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignLeft="@+id/editText2"
android:layout_marginBottom="37dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="17dp"
android:text="+" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="14dp"
android:text="*" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="/" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/textView1"
android:text="-" />
</RelativeLayout>
MainActivity.java
package com.example.simplecalculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button plus,sub,mult,div;
EditText fno,sno,result;
int a,b,c;
String m,n;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plus=(Button)findViewById(R.id.button1);
sub=(Button)findViewById(R.id.button2);
fno=(EditText)findViewById(R.id.editText1);
sno=(EditText)findViewById(R.id.editText2);
result=(EditText)findViewById(R.id.editText3);
plus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a+b;
result.setText(Integer.toString(c));
}
}
});
sub=(Button)findViewById(R.id.button2);
sub.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a-b;
result.setText(Integer.toString(c));
}
}
});
mult=(Button)findViewById(R.id.button3);
mult.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a*b;
result.setText(Integer.toString(c));
}
}
});
div=(Button)findViewById(R.id.button4);
div.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both The Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a/b;
result.setText(Integer.toString(c));
}
}
});
}
}
When user presses the Button without entering the values,there is a chance of getting an application error, to avoid this the following code is used above
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both The Numbers",Toast.LENGTH_SHORT).show();
}
The Output of the application is like this :
0 comments:
Post a Comment