How to create SIMPLE CALCULATOR in Android Studio || create a calculator in android
How to create a simple calculator source code is given below:
Visit our YouTube channel:https://www.youtube.com/channel/UCsOEG3w9cycc41J-pgF3S7g
Here is the XML and Main Activity Code of Calculator. You have to just change the name of Package of
the Source 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"
style="bold"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="292dp"
android:layout_height="42dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:text="MH Computer Academy"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="295dp"
android:layout_height="43dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:ems="10"
android:hint="Result "
android:inputType="textPersonName"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="2"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btn3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="1"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="3"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btnAdd"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="+"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn3"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="4"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btn5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="5"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btn6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn4"
app:layout_constraintTop_toBottomOf="@+id/btn2" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="6"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btnSub"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn5"
app:layout_constraintTop_toBottomOf="@+id/btn3" />
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="-"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn6"
app:layout_constraintTop_toBottomOf="@+id/btnAdd" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="7"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btn8"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn4" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="8"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btn9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn7"
app:layout_constraintTop_toBottomOf="@+id/btn5" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="9"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btnMul"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn8"
app:layout_constraintTop_toBottomOf="@+id/btn6" />
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="*"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn9"
app:layout_constraintTop_toBottomOf="@+id/btnSub" />
<Button
android:id="@+id/btnDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="."
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btn0"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn7" />
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="0"
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btnEqual"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnDot"
app:layout_constraintTop_toBottomOf="@+id/btn8" />
<Button
android:id="@+id/btnEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="="
android:textSize="30dp"
app:layout_constraintEnd_toStartOf="@+id/btnDiv"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn0"
app:layout_constraintTop_toBottomOf="@+id/btn9" />
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="/"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnEqual"
app:layout_constraintTop_toBottomOf="@+id/btnMul" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="160dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="159dp"
android:layout_marginRight="159dp"
android:text="C"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn0" />
</androidx.constraintlayout.widget.ConstraintLayout>
............................................................................
Here is the Code of Main Activity
package com.mhca.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
// We have to create Objects of All the Button that we have made.
// To Click on Alt + Enter to import the Package of Button if Not import.
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnAdd, btnSub,btnMul,btnDiv, btnEqual, btnDot;
Button btnClear; // these are the names of variables that we can same or different to the XML labels like buttons etc
EditText ed1;
float Res1, Res2;
boolean Add, Sub, Mul , Div; // Taking Variable's to Show if any body click on any button so, these will be used in code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// We have to mapp the whole variables with the XML's Button so we will write the given code.
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn6=(Button)findViewById(R.id.btn6);
btn7=(Button)findViewById(R.id.btn7);
btn8=(Button)findViewById(R.id.btn8);
btn9=(Button)findViewById(R.id.btn9);
btn0=(Button)findViewById(R.id.btn0);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnSub=(Button)findViewById(R.id.btnSub);
btnMul=(Button)findViewById(R.id.btnMul);
btnDiv=(Button)findViewById(R.id.btnDiv);
btnDot=(Button)findViewById(R.id.btnDot);
btnEqual=(Button)findViewById(R.id.btnEqual);
btnClear=(Button)findViewById(R.id.btnClear);
ed1=(EditText)findViewById(R.id.editText);
// Now we are doing some thing change that is "when we click on any button that will shown in Edit Text
// we will write just btn1.setOnClickListener(new View) After that Suggestion will come and Click on it (...)
// gettext mean we have to add 1 into the Edit Text if it has some Number or Value.
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"5");
}
}); btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"6");
}
}); btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"9");
}
});
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"0");
}
});
// if any one click's on any Operation the we have to write the same code with smaller modification.
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If Edit Text Has Stored Nothing then we will write as
if(ed1==null)
{
ed1.setText("");
}
else // Or if Edit Text has some number then we will write this code
{
// we will store number in Res1 variable on which user has clicked
Res1=Float.parseFloat(ed1.getText() + "");
Add=true; // Add is a variable that we have taken of Boolean Type.
ed1.setText(null); // Mean if Number is already showing in Edit Text after clicking value will vanish due to this line
}
}
});
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If Edit Text Has Stored Nothing then we will write as
if(ed1==null)
{
ed1.setText("");
}
else // Or if Edit Text has some number then we will write this code
{
// we will store number in Res1 variable on which user has clicked
Res1=Float.parseFloat(ed1.getText() + "");
Sub=true; // Sub is a variable that we have taken of Boolean Type.
ed1.setText(null); // Mean if Number is already showing in Edit Text after clicking value will vanish due to this line
}
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If Edit Text Has Stored Nothing then we will write as
if(ed1==null)
{
ed1.setText("");
}
else // Or if Edit Text has some number then we will write this code
{
// we will store number in Res1 variable on which user has clicked
Res1=Float.parseFloat(ed1.getText() + "");
Mul=true; // Mul is a variable that we have taken of Boolean Type.
ed1.setText(null); // Mean if Number is already showing in Edit Text after clicking value will vanish due to this line
}
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If Edit Text Has Stored Nothing then we will write as
if(ed1==null)
{
ed1.setText("");
}
else // Or if Edit Text has some number then we will write this code
{
// we will store number in Res1 variable on which user has clicked
Res1=Float.parseFloat(ed1.getText() + "");
Div=true; // Div is a variable that we have taken of Boolean Type.
ed1.setText(null); // Mean if Number is already showing in Edit Text after clicking value will vanish due to this line
}
}
});
// Now User will click on an other number to complete the operation like ( 12 + ...=)
// So Result will show after clicking on the Equal Button
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Res2=Float.parseFloat(ed1.getText() + ""); // this will get the other number that user will give after clicking on operator
// Now we will see that which button user has Clicked
if(Add==true){
ed1.setText(Res1+Res2+"");
Add=false; // Work of Add operation has completed so we made this false
} // copy and past the same if condition with little modification
if(Sub==true){
ed1.setText(Res1-Res2+"");
Sub=false; // Work of Sub operation has completed so we made this false
}
if(Mul==true){
ed1.setText(Res1*Res2+"");
Add=false; // Work of Mul operation has completed so we made this false
}
if(Div==true){
ed1.setText(Res1/Res2+"");
Div=false; // Work of Div operation has completed so we made this false
}
}
});
// we will write some code for Clean Button
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText("");
}
});
}
}// I hope you will enjoyed your learnings and Follow our Blogspot site
Comments
Post a Comment