Calculator app tutorial in Android Studio
In this article we have provided code for a simple android calculator. We have uploaded 4 videos on YouTube for this Calculator app tutorial. We have moved step by step in those tutorials.
First create a back.xml inside drawable as vector assets (What is vector assets and how to create).
Codes:
Main Activity:
XML Code -
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="@+id/prev"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Previous"
android:textSize="30dp"
android:textColor="@color/lightblue"
android:padding="20dp"/>
<TextView
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Main"
android:textSize="30dp"
android:textColor="@color/blue"
android:padding="20dp"/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5"
android:layout_gravity="center">
<ImageButton
android:id="@+id/back"
android:layout_height="50dp"
android:layout_width="88dp"
android:src="@drawable/back"
android:padding="10dp"
android:layout_margin="5dp"
android:layout_column="3"
android:elevation="3dp"
android:background="@color/lightblue"/>
<Button
android:id="@+id/b7"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="7"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="8"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b9"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="9"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/bplus"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/lightblue"
android:text="+"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="4"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b5"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="5"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b6"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="6"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/bminus"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/lightblue"
android:text="-"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="1"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="2"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="3"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/bmulti"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/lightblue"
android:text="*"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/b0"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="0"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/bdot"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="."
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/bequal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/white"
android:text="="
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
<Button
android:id="@+id/bdivide"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/lightblue"
android:text="/"
android:textSize="30dp"
android:layout_margin="5dp"
android:elevation="10dp"/>
</GridLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA Code -
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,plus,minus,divide,mult,dot,equal;
ImageButton back;
TextView prev,main;
int op_index = 0 ;
int op_count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 = findViewById(R.id.b0);
b1 = findViewById(R.id.b1);
b2 = findViewById(R.id.b2);
b3 = findViewById(R.id.b3);
b4 = findViewById(R.id.b4);
b5 = findViewById(R.id.b5);
b6 = findViewById(R.id.b6);
b7 = findViewById(R.id.b7);
b8 = findViewById(R.id.b8);
b9 = findViewById(R.id.b9);
dot = findViewById(R.id.bdot);
plus = findViewById(R.id.bplus);
minus = findViewById(R.id.bminus);
mult = findViewById(R.id.bmulti);
divide = findViewById(R.id.bdivide);
equal = findViewById(R.id.bequal);
back = findViewById(R.id.back);
prev = findViewById(R.id.prev);
main = findViewById(R.id.main);
main.setText("");
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"0");
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+"9");
}
});
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
char c = main.getText().toString().charAt(main.getText().length()-1);
if(main.getText().length() == 0){
Toast.makeText(getApplicationContext(),"Enter a operand first",Toast.LENGTH_SHORT).show();
}
else if(isOperator(c)){
Toast.makeText(getApplicationContext(),"Cannot enter two operator",Toast.LENGTH_SHORT).show();
}
else{
if(op_count == 1){
prev.setText(main.getText().toString());
main.setText(evaluate(main.getText().toString(),op_index)+"");
op_count--;
}
op_index = main.getText().length();
main.setText(main.getText()+"+");
op_count++;
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
char c = main.getText().toString().charAt(main.getText().length()-1);
if(main.getText().length() == 0){
Toast.makeText(getApplicationContext(),"Enter a operand first",Toast.LENGTH_SHORT).show();
}
else if(isOperator(c)){
Toast.makeText(getApplicationContext(),"Cannot enter two operator",Toast.LENGTH_SHORT).show();
}
else{
if(op_count == 1){
prev.setText(main.getText().toString());
main.setText(evaluate(main.getText().toString(),op_index)+"");
op_count--;
}
op_index = main.getText().length();
main.setText(main.getText()+"-");
op_count++;
}
}
});
mult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
char c = main.getText().toString().charAt(main.getText().length()-1);
if(main.getText().length() == 0){
Toast.makeText(getApplicationContext(),"Enter a operand first",Toast.LENGTH_SHORT).show();
}
else if(isOperator(c)){
Toast.makeText(getApplicationContext(),"Cannot enter two operator",Toast.LENGTH_SHORT).show();
}
else{
if(op_count == 1){
prev.setText(main.getText().toString());
main.setText(evaluate(main.getText().toString(),op_index)+"");
op_count--;
}
op_index = main.getText().length();
main.setText(main.getText()+"*");
op_count++;
}
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
char c = main.getText().toString().charAt(main.getText().length()-1);
if(main.getText().length() == 0){
Toast.makeText(getApplicationContext(),"Enter a operand first",Toast.LENGTH_SHORT).show();
}
else if(isOperator(c)){
Toast.makeText(getApplicationContext(),"Cannot enter two operator",Toast.LENGTH_SHORT).show();
}
else{
if(op_count == 1){
prev.setText(main.getText().toString());
main.setText(evaluate(main.getText().toString(),op_index)+"");
op_count--;
}
op_index = main.getText().length();
main.setText(main.getText()+"/");
op_count++;
}
}
});
dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.setText(main.getText()+".");
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(main.getText().toString().length() != 0){
char last;
last = main.getText().toString().charAt(main.getText().length()-1);
if(last == '+' || last == '-' || last == '*' || last == '/'){
op_count--;
}
if(!main.getText().toString().isEmpty()){
main.setText(main.getText().toString().substring(0,main.getText().length()-1));
}
}
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(main.getText().length() != 0){
if(op_count == 0){
main.setText(main.getText().toString());
}else{
char last;
last = main.getText().toString().charAt(main.getText().length()-1);
if(isOperator(last)){
Toast.makeText(getApplicationContext(),"Cannot Evaluate\n Incorrect equation",Toast.LENGTH_SHORT).show();
}else{
prev.setText(main.getText().toString());
main.setText(evaluate(main.getText().toString(),op_index)+"");
op_count--;
}
}
}
}
});
}
public boolean isOperator(char c){
if(c == '+' || c == '-' || c == '/' || c == '*'){
return true;
}else{
return false;
}
}
public double evaluate(String s, int op_index){
double first,last;
String operator;
first = Double.parseDouble(s.substring(0,op_index));
operator = s.substring(op_index,op_index+1);
last = Double.parseDouble(s.substring(op_index+1));
switch (operator){
case "+":
return first+last;
case "-":
return first-last;
case "*":
return first*last;
case "/":
return first/last;
}
return 0;
}
}
Result -
Download Calculator app apk file DownloadYouTube videos link
Part-1Part-3
No comments:
Post a Comment