Most recent
Posted by
Rishwi Binnu
on
- Get link
- X
- Other Apps
Android calculator app
In ✋5 steps Within ✋5 minutes
Requirements:
1.Android Studio
2.Java (Installed correctly)
3.Mainly a PC or a Laptop...😛
The sequence of steps:
Step 1: create a new project in Android Studio.
Step 2: keep XML and java file as it is.
Step 3: copy the XML code in activity_main.xml
Step 4: copy the java code in MainActivity.java
Step 5: click RUN(Play Button)and run the app using the emulator or your smartphone.
***Screenshots are provided below the code.
XML File :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.rishwibinnu.caluculator.MainActivity">
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:text="9" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:layout_alignBaseline="@+id/btn9"
android:layout_alignBottom="@+id/btn9"
android:layout_toRightOf="@+id/btn9"
android:layout_toEndOf="@+id/btn9" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:layout_alignBaseline="@+id/btn8"
android:layout_alignBottom="@+id/btn8"
android:layout_toRightOf="@+id/btn8"
android:layout_toEndOf="@+id/btn8" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn9"
android:layout_alignStart="@+id/btn9"
android:layout_below="@+id/btn9"
android:layout_marginTop="10dp"
android:text="6" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn8"
android:layout_alignStart="@+id/btn8"
android:layout_alignTop="@+id/btn6"
android:text="5" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_alignBaseline="@+id/btn5"
android:layout_alignBottom="@+id/btn5"
android:layout_alignLeft="@+id/btn7"
android:layout_alignStart="@+id/btn7" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn6"
android:layout_alignStart="@+id/btn6"
android:layout_below="@+id/btn6"
android:layout_marginTop="14dp"
android:text="3" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn5"
android:layout_alignStart="@+id/btn5"
android:layout_alignTop="@+id/btn3"
android:text="2" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn4"
android:layout_alignStart="@+id/btn4"
android:layout_alignTop="@+id/btn2"
android:text="1" />
<Button
android:id="@+id/btnclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn3"
android:layout_alignStart="@+id/btn3"
android:layout_below="@+id/btn3"
android:layout_marginTop="17dp"
android:text="clear" />
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnclear"
android:layout_alignLeft="@+id/btn2"
android:layout_alignStart="@+id/btn2"
android:text="0" />
<Button
android:id="@+id/btnequal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn1"
android:layout_alignStart="@+id/btn1"
android:layout_alignTop="@+id/btn0"
android:text="=" />
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn7"
android:layout_toEndOf="@+id/btn7"
android:layout_toRightOf="@+id/btn7"
android:text="+" />
<Button
android:id="@+id/btnsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn4"
android:layout_toEndOf="@+id/btn4"
android:layout_toRightOf="@+id/btn4"
android:text="-" />
<Button
android:id="@+id/btnmul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn1"
android:layout_alignBottom="@+id/btn1"
android:layout_toEndOf="@+id/btn1"
android:layout_toRightOf="@+id/btn1"
android:text="*" />
<Button
android:id="@+id/btndiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:layout_alignBaseline="@+id/btnequal"
android:layout_alignBottom="@+id/btnequal"
android:layout_toRightOf="@+id/btnequal"
android:layout_toEndOf="@+id/btnequal" />
<TextView
android:id="@+id/tvcontrol"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:textSize="24sp" />
<TextView
android:id="@+id/tvresult"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_below="@+id/tvcontrol"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:textStyle="bold" />
</RelativeLayout>
Java File :
package com.example.rishwibinnu.caluculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private Button zero;
private Button add;
private Button sub;
private Button mul;
private Button div;
private Button equal;
private Button clear;
private TextView info;
private TextView result;
private final char ADDITION = '+';
private final char SUBTRACTION = '-';
private final char MULTIPLICATION = '*';
private final char DIVISION = '/';
private final char EQU = 0;
private double val1=Double.NaN;
private double val2;
private char ACTION;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUIViews();
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "0");
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MobileAds.initialize(MainActivity.this, "ca-app-pub-9449308388500357~2044595441");
compute();
ACTION = ADDITION;
result.setText(String.valueOf(val1) + "+");
info.setText(null);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = SUBTRACTION;
result.setText(String.valueOf(val1) + "-");
info.setText(null);
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = MULTIPLICATION;
result.setText(String.valueOf(val1) + "*");
info.setText(null);
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = DIVISION;
result.setText(String.valueOf(val1) + "/");
info.setText(null);
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = EQU;
result.setText(result.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1));
info.setText(null);
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(info.getText().length() > 0){
CharSequence name = info.getText().toString();
info.setText(name.subSequence(0, name.length()-1));
}
else {
val1=Double.NaN;
val2=Double.NaN;
info.setText(null);
result.setText(null);
}
}
});
}
private void setupUIViews()
{
one = (Button)findViewById(R.id.btn1);
two = (Button)findViewById(R.id.btn2);
three = (Button)findViewById(R.id.btn3);
four = (Button)findViewById(R.id.btn4);
five = (Button)findViewById(R.id.btn5);
six = (Button)findViewById(R.id.btn6);
seven = (Button)findViewById(R.id.btn7);
eight = (Button)findViewById(R.id.btn8);
nine = (Button)findViewById(R.id.btn9);
zero = (Button)findViewById(R.id.btn0);
add = (Button)findViewById(R.id.btnadd);
sub = (Button)findViewById(R.id.btnsub);
mul = (Button)findViewById(R.id.btnmul);
div = (Button)findViewById(R.id.btndiv);
equal = (Button)findViewById(R.id.btnequal);
clear = (Button)findViewById(R.id.btnclear);
info = (TextView)findViewById(R.id.tvcontrol);
result = (TextView)findViewById(R.id.tvresult);
}
private void compute()
{
if(!Double.isNaN(val1)){
val2 = Double.parseDouble(info.getText().toString());
switch (ACTION){
case ADDITION:
val1 = val1 + val2;
break;
case SUBTRACTION:
val1 = val1 - val2;
break;
case MULTIPLICATION:
val1 = val1 * val2;
break;
case DIVISION:
val1 = val1 / val2;
break;
case EQU:
break;
}
}
else {
val1=Double.parseDouble(info.getText().toString());
}
}
}
Screenshots of the Calculator app:
1
2
3
4
- Get link
- X
- Other Apps
Comments
Post a Comment