Introduction
In this article, I will create a small application for a mini calculator. Whenever we want to just add, subtract, multiply, or divide two numbers, we usually want to use a mini calculator. So I am telling you how to create a mini calculator in Android. The following procedure shows how to create a mini calculator on Android.
Step 1
First of all create a new Android application project using: "File" -> "New"-> "Android Application Project" as shown below.
Step 2
Now open the "activity_main.xml" file and update it for the mini calculator UI screen as in the following code.
- <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"
- tools:context=".MainActivity" >
- <EditText
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:ems="10"
- android:maxLength="10"
- android:inputType="numberDecimal" />
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/editText1"
- android:ems="10"
- android:maxLength="10"
- android:inputType="numberDecimal" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/button1"
- android:text="@string/result"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <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_alignParentRight="true"
- android:text="@string/divdbtn" />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/textView1"
- android:layout_marginRight="16dp"
- android:layout_toLeftOf="@+id/button4"
- android:text="@string/multbtn" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/textView1"
- android:layout_marginRight="18dp"
- android:layout_toLeftOf="@+id/button3"
- android:text="@string/subbtn" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/editText2"
- android:layout_marginRight="14dp"
- android:layout_toLeftOf="@+id/button2"
- android:text="@string/addbtn" />
- </RelativeLayout>
Step 3
After updating the UI screen in "activity_main.xml" update "strings.xml" witch stores all the strings. Update it as in the following code.
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">MiniClc</string>
- <string name="hello_world">Hello world!</string>
- <string name="menu_settings">Settings</string>
- <string name="result"></string>
- <string name="clc">Calculate</string>
- <string name="addbtn">Add</string>
- <string name="subbtn">Sub</string>
- <string name="multbtn">mult</string>
- <string name="divdbtn">divd</string>
- </resources>
Step 4
Now open the "MainActivity.java" file from "src/com.mcn.minicalculator/MainActivity.java" and update it as in the following code.
- package com.example.miniclc;
- import android.app.Activity;
- import android.os.Bundle;
- 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 {
- double a,b,c;
- Button addbtn,subbtn,mulbtn,divdbtn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final EditText edt1=(EditText) findViewById(R.id.editText1);
- final EditText edt2=(EditText) findViewById(R.id.editText2);
- final TextView result= (TextView) findViewById(R.id.textView1);
- addbtn=(Button) findViewById(R.id.button1);
- subbtn=(Button) findViewById(R.id.button2);
- mulbtn=(Button) findViewById(R.id.button3);
- divdbtn=(Button) findViewById(R.id.button4);
- addbtn.setOnClickListener(new Button.OnClickListener()
- {public void onClick
- (View v) { calculate();}
- private void calculate() {
-
- a=Double.parseDouble(edt1.getText().toString());
- b=Double.parseDouble(edt2.getText().toString());
- c=a+b;
- result.setText(Double.toString(c));
- }});
- subbtn.setOnClickListener(new Button.OnClickListener()
- {public void onClick
- (View v) { calculate();}
- private void calculate() {
-
- a=Double.parseDouble(edt1.getText().toString());
- b=Double.parseDouble(edt2.getText().toString());
- c=a-b;
- result.setText(Double.toString(c));
- }});
- mulbtn.setOnClickListener(new Button.OnClickListener()
- {public void onClick
- (View v) { calculate();}
- private void calculate() {
-
- a=Double.parseDouble(edt1.getText().toString());
- b=Double.parseDouble(edt2.getText().toString());
- c=a*b;
- result.setText(Double.toString(c));
- }});
- divdbtn.setOnClickListener(new Button.OnClickListener()
- {public void onClick
- (View v) { calculate();}
- private void calculate() {
-
- a=Double.parseDouble(edt1.getText().toString());
- b=Double.parseDouble(edt2.getText().toString());
- c=a/b;
- result.setText(Double.toString(c));
- }});
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
Output
The output will show on the current screen where you enter the query.
If you click on the "Add" button then you will get the addition of both numbers.
And if you click on "Sub" then the result will be the subtraction.
And if you click on "mult" then you will get the multiplication.
And after clicking on "divd" you will get the division.