How to make a Calculator in Android Studio || Simple Calculator In Android Studio || RSM Developer

Simple Calculator In Android Studio 


 Step 1Creating a new project

  • Open a new project.
  • We will be working on Empty Activity with language as Java. Leave all other options unchanged.
  • You can change the name of the project at your convenience.
  • There will be two default files named activity_main.xml and MainActivity.java.

Step 2: Open res -> layout ->activity_main.xml (or) main.xml and add following code:

In this step we open an XML file and add the code :-

<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"
android:background="@color/white"
tools:context=".MainActivity5">


<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:background="@drawable/bottom_shade"
android:paddingVertical="16dp"
android:layout_margin="5dp"
android:layout_above="@id/logo"
>
<TextView
android:id="@+id/inputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="20dp"
android:textColor="#000000"
android:textSize="20dp"
android:layout_margin="5dp"
android:layout_above="@id/outputText"/>


<TextView
android:id="@+id/outputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_above="@id/buttons_layout"
android:textColor="@color/teal_200"
android:layout_margin="5dp"
android:textSize="25dp" />
</LinearLayout>

<TextView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bondhu Calculator"
android:textSize="20sp"
android:textColor="@color/teal_700"
android:layout_above="@+id/buttons_layout"
android:gravity="center"
/>

<LinearLayout
android:id="@+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="#F1F1F1"
android:paddingVertical="16dp"
>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4"
android:padding="5dp"
>


<Button
android:id="@+id/clear_btn"
android:text="C"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#F44336"
android:layout_weight="1"
android:onClick="onButtonClicked"/>


<Button
android:id="@+id/power_btn"
android:text="^"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#0D47A1"
android:layout_weight="1"
android:onClick="onButtonClicked"/>

<Button
android:id="@+id/percent_btn"
android:text="%"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#0D47A1"
android:layout_weight="1"
android:onClick="onButtonClicked"
/>



<Button
android:id="@+id/division_btn"
android:text="÷"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#0D47A1"
android:layout_weight="1"
android:onClick="onButtonClicked"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4"
>

<Button
android:id="@+id/btn7"
android:text="7"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#2196F3"
android:layout_weight="1"
android:onClick="onButtonClicked"/>

<Button
android:id="@+id/btn8"
android:text="8"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#009688"
android:layout_weight="1"
android:onClick="onButtonClicked"/>


<Button
android:id="@+id/btn9"
android:text="9"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#009688"
android:layout_weight="1"
android:onClick="onButtonClicked"/>

<Button
android:id="@+id/btn_multiply"
android:text="×"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#00BCD4"
android:layout_weight="1"
android:onClick="onButtonClicked"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4"
>

<Button
android:id="@+id/btn4"
android:text="4"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#2196F3"
android:layout_weight="1"
android:onClick="onButtonClicked"/>


<Button
android:id="@+id/btn5"
android:text="5"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#009688"
android:layout_weight="1"
android:onClick="onButtonClicked"/>

<Button
android:id="@+id/btn6"

android:text="6"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#009688"
android:layout_weight="1"
android:onClick="onButtonClicked"/>

<Button
android:id="@+id/btn_minus"
android:text="-"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#00BCD4"
android:layout_weight="1"
android:onClick="onButtonClicked"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4"
>

<Button
android:id="@+id/btn1"
android:text="1"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#2196F3"
android:onClick="onButtonClicked"
android:layout_weight="1"
/>

<Button
android:id="@+id/btn2"
android:text="2"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#009688"
android:onClick="onButtonClicked"
android:layout_weight="1"/>


<Button
android:id="@+id/btn3"
android:text="3"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#009688"
android:onClick="onButtonClicked"
/>

<Button
android:id="@+id/btn_plus"
android:text="+"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#00BCD4"
android:onClick="onButtonClicked"
android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:weightSum="3"
android:orientation="horizontal"
>

<Button
android:id="@+id/btn0"
android:text="0"
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#FF9800"
android:layout_weight="1"
android:onClick="onButtonClicked"/>

<Button
android:id="@+id/btn_dot"
android:text="."
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#FF9800"
android:onClick="onButtonClicked"
android:layout_weight="1"
/>

<Button
android:id="@+id/btn_equals"
android:text="="
android:layout_width="72dp"
android:layout_height="72dp"
android:textSize="32sp"
android:textColor="@color/colorWhite"
app:cornerRadius="36dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_margin="12dp"
android:backgroundTint="#FF9800"
android:onClick="onButtonClicked"
android:layout_weight="1"
/>



</LinearLayout>

</LinearLayout>
</RelativeLayout>


Step 3: Create a Custom drawable resource file (Name:bottom_shade) :-Open ->drawable -> bottom_shade.xml  and add following code:

In this step we open an XML file and add the code :-

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


<item>
<shape android:shape="rectangle" >
<solid android:color="#004D40" />
<corners android:radius="10dp" />
</shape>
</item>


<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">


<shape android:shape="rectangle" >
<solid android:color="#D58AB3F3" />
<corners android:radius="30dp"/>
</shape>

<shape android:shape="rectangle" >
<corners android:radius="0dip" />
<stroke android:width="0dip"
android:color="#DD1313" />


</shape>


</item>

</layer-list>


Step 4: Open Java -> package – > MainActivity.Java and add following code:

In this step we open an Java file and add the code :-

package com.rsmdeveloper.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
* Created by RSM Developer on 14-03-2023.
* Follow Facebook : https://www.facebook.com/RSMDeveloper
* Subscribe Youtube : https://www.youtube.com/@RSMDeveloper
* Visit Website : https://rsmdeveloper.blogspot.com/
* Develope Your Creativity With RSM Developer
**/

public class MainActivity5 extends AppCompatActivity {

//===============================================================
TextView inputText, outputText;

private String input, output, newOutput;

private Button clear_btn,power_btn,percent_btn,division_btn,
btn7,btn8,btn9,btn4,btn5,btn6,btn1,btn2,btn3,btn0,
btn_multiply, btn_minus, btn_plus, btn_equals,btn_dot;
//===============================================================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);

//===============================================================
inputText = findViewById(R.id.inputText);
outputText = findViewById(R.id.outputText);

btn0= findViewById(R.id.btn0);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn9);
btn_plus= findViewById(R.id.btn_plus);
btn_multiply= findViewById(R.id.btn_multiply);
division_btn = findViewById(R.id.division_btn);
btn_minus = findViewById(R.id. btn_minus);
btn_dot = findViewById(R.id.btn_dot);
power_btn = findViewById(R.id.power_btn);
btn_equals = findViewById(R.id.btn_equals);
percent_btn= findViewById(R.id.percent_btn);
clear_btn = findViewById(R.id.clear_btn);

}
//===============================================================
public void onButtonClicked(View view) {

Button button = (Button) view;
String data = button.getText().toString();
switch (data) {
case "C":
input = null;
output=null;
newOutput=null;
outputText.setText("");
break;

case "^":
solve();
input += "^";
break;
case "*":
solve();
input += "*";
break;

case "=":
solve();
break;

case "%":
input += "%";
double d = Double.parseDouble(inputText.getText().toString()) / 100;
outputText.setText(String.valueOf(d));
break;

default:
if (input == null) {
input = "";
}
if (data.equals("+") || data.equals("/") || data.equals("-")) {
solve();
}
input += data;
}
inputText.setText(input);
}

private void solve() {
if (input.split("\\+").length == 2) {
String numbers[] = input.split("\\+");
try {
double d = Double.parseDouble(numbers[0]) + Double.parseDouble(numbers[1]);
output = Double.toString(d);
newOutput = cutDecimal(output);
outputText.setText(newOutput);
input = d +"";
}catch (Exception e) {
outputText.setText(e.getMessage().toString());
}
}
if (input.split("\\*").length == 2) {
String numbers[] = input.split("\\*");
try {
double d = Double.parseDouble(numbers[0]) * Double.parseDouble(numbers[1]);
output = Double.toString(d);
newOutput = cutDecimal(output);
outputText.setText(newOutput);
input = d +"";
}catch (Exception e){
outputText.setText(e.getMessage().toString());
}
}
if (input.split("\\/").length == 2) {
String numbers[] = input.split("\\/");
try {
double d = Double.parseDouble(numbers[0]) / Double.parseDouble(numbers[1]);
output = Double.toString(d);
newOutput = cutDecimal(output);
outputText.setText(newOutput);
input = d +"";
}catch (Exception e){
outputText.setText(e.getMessage().toString());
}
}
if (input.split("\\^").length == 2) {
String numbers[] = input.split("\\^");
try {
double d = Math.pow(Double.parseDouble(numbers[0]), Double.parseDouble(numbers[1]));
output = Double.toString(d);
newOutput = cutDecimal(output);
outputText.setText(newOutput);
input = d +"";
}catch (Exception e){
outputText.setText(e.getMessage().toString());
}
}
if (input.split("\\-").length == 2) {
String numbers[] = input.split("\\-");
try {
if (Double.parseDouble(numbers[0]) < Double.parseDouble(numbers[1])){
double d = Double.parseDouble(numbers[1]) - Double.parseDouble(numbers[0]);
output = Double.toString(d);
newOutput = cutDecimal(output);
outputText.setText("-" + newOutput);
input = d +"";
}
else {
double d = Double.parseDouble(numbers[0]) - Double.parseDouble(numbers[1]);
output = Double.toString(d);
newOutput = cutDecimal(output);
outputText.setText(newOutput);
input = d + "";
}
}catch (Exception e){
outputText.setText(e.getMessage().toString());
}
}
//===============================================================
}
private String cutDecimal(String number){
String n [] = number.split("\\.");
if (n.length >1){
if (n[1].equals("0")){
number = n[0];
}
}
return number;
}//===============================================================
}


Output:

Now run the App and you will see main topics and sub-topics listed.....

Watch this video


Post a Comment

Previous Post Next Post

যোগাযোগ ফর্ম