Sunday, August 9, 2020

How to Create Calculator Android App Using Android Studio.

Calculator Android App Using Android Studio.


  • Building a Simple Calculator using Android Studio.
  • Android Development: Creating a Basic Calculator.
  • Create Simple Calculator Android Application.
  • How to create a Simple Calculator Application for Android Studio.

MainActivity.java


package com.example.sunny.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

double input1 = 0, input2 = 0;
TextView edt1;
boolean Addition, Subtract, Multiplication, Division, Remainder1, decimal;
Button btn0, btn1, btn2, btn3, btn4,
btn5, btn6, btn7, btn8, btn9, btnAdd, btnSub,
btnMul, btnDivision, btnEqual, btnDel, btnDot, Remainder;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn0 = (Button) findViewById(R.id.button0);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
btn5 = (Button) findViewById(R.id.button5);
btn6 = (Button) findViewById(R.id.button6);
btn7 = (Button) findViewById(R.id.button7);
btn8 = (Button) findViewById(R.id.button8);
btn9 = (Button) findViewById(R.id.button9);
btnDot = (Button) findViewById(R.id.buttonDot);
btnAdd = (Button) findViewById(R.id.buttonadd);
btnSub = (Button) findViewById(R.id.buttonsub);
btnMul = (Button) findViewById(R.id.buttonmul);
btnDivision = (Button) findViewById(R.id.buttondiv);
Remainder = (Button) findViewById(R.id.Remainder);
btnDel = (Button) findViewById(R.id.buttonDl);
btnEqual = (Button) findViewById(R.id.buttoneql);
edt1 = (TextView) findViewById(R.id.display);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "1");
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "2");
}
});

btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "3");
}
});

btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "4");
}
});

btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "5");
}
});

btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "6");
}
});

btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "7");
}
});

btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "8");
}
});

btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "9");
}
});

btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText(edt1.getText() + "0");
}
});

btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Addition = true;
decimal = false;
edt1.setText(null);
}
}
});

btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Multiplication = true;
decimal = false;
edt1.setText(null);
}
}
});

btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Subtract = true;
decimal = false;
edt1.setText(null);
}
}
});

btnDivision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Division = true;
decimal = false;
edt1.setText(null);
}
}
});

Remainder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt1.getText().length() != 0) {
input1 = Float.parseFloat(edt1.getText() + "");
Remainder1 = true;
decimal = false;
edt1.setText(null);
}
}
});

btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Addition || Multiplication || Subtract || Division || Remainder1) {
input2 = Float.parseFloat(edt1.getText() + "");
}

if (Addition) {

edt1.setText(input1 + input2 + "");
Addition = false;
}

if (Multiplication) {
edt1.setText(input1 * input2 + "");
Multiplication = false;
}

if (Subtract) {
edt1.setText(input1 - input2 + "");
Subtract = false;
}

if (Division) {
edt1.setText(input1 / input2 + "");
Division = false;
}
if (Remainder1) {
edt1.setText(input1 % input2 + "");
Remainder1 = false;
}
}
});

btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt1.setText("");
input1 = 0;
input2 = 0;
}
});

btnDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (decimal) {
//do nothing or you can show the error
} else {
edt1.setText(edt1.getText() + ".");
decimal = true;
}

}
});


}
}


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#ecf0f1"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="horizontal">

<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ecf6f9"
android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">

<Button
android:id="@+id/buttonDl"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/button"
android:text="Del"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/buttoneql"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/button"
android:text="="
android:textColor="#ecf0f1"
android:textSize="30sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="1"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="2"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="3"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/buttondiv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/operator_selector"
android:text="÷"
android:textColor="#ecf0f1"
android:textSize="35dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="4"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="5"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="6"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/buttonsub"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/operator_selector"
android:text="x"
android:textColor="#ecf0f1"
android:textSize="30sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="7"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="8"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="9"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/buttonmul"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/operator_selector"
android:text="-"
android:textColor="#ecf0f1"
android:textSize="30sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="horizontal">

<Button
android:id="@+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="."
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/selector"
android:text="0"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/Remainder"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/operator_selector"
android:text="%"
android:textColor="#ecf0f1"
android:textSize="30sp" />

<Button
android:id="@+id/buttonadd"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@drawable/operator_selector"

android:text="+"
android:textColor="#ecf0f1"
android:textSize="30sp" />


</LinearLayout>

</LinearLayout>


button.xml


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">

<solid android:color="#3498db" />
<corners android:radius="30px"/>

<padding
android:left="5dp"
android:top="5dp"
android:right="3dp"
android:bottom="3dp"/>

</shape>


operator_selector.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:state_focused="false">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="30px"/>
<solid android:color="@color/green"/>

<padding
android:left="5dp"
android:top="5dp"
android:right="3dp"
android:bottom="3dp"/>
</shape>
</item>

<item android:state_pressed="false"
android:state_focused="true">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="30px"/>
<solid android:color="@color/operator_btn_clr"/>

<padding
android:left="5dp"
android:top="5dp"
android:right="3dp"
android:bottom="3dp"/>
</shape>
</item>

<item android:state_pressed="true"
android:state_focused="false">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="30px"/>
<solid android:color="@color/operator_btn_clr"/>

<padding
android:left="5dp"
android:top="5dp"
android:right="3dp"
android:bottom="3dp"/>
</shape>
</item>

</selector>


selector.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="false"
android:state_focused="false">

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="30px"/>
<solid android:color="@color/btn_clr"/>

<padding
android:left="5dp"
android:top="5dp"
android:right="3dp"
android:bottom="3dp"/>
</shape>
</item>

<item android:state_pressed="false"
android:state_focused="true">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="30px"/>
<solid android:color="@color/selected"/>

<padding
android:left="5dp"
android:top="5dp"
android:right="3dp"
android:bottom="3dp"/>
</shape>
</item>

<item android:state_pressed="true"
android:state_focused="false">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="30px"/>
<solid android:color="@color/selected"/>

<padding
android:left="5dp"
android:top="5dp"
android:right="3dp"
android:bottom="3dp"/>
</shape>
</item>

</selector>


OUTPUT

Create Simple Calculator Android App Using Android Studio.
Calculator Android App Using Android Studio.




No comments:

Post a Comment

Please do not any spam link in Comment Box