Wednesday, 10 February 2021

Calculator in android studio with Source Code | Calculator in Android studio | Clcoding

 Activity Main :
<?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="#000"
tools:context=".MainActivity">


<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="1"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="2"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button3"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="3"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="4"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="5"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button6"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="6"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="7"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button8"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="8"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button9"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="9"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button0"
android:layout_width="185dp"
android:layout_height="80dp"
android:background="@drawable/new_but"
android:paddingRight="95sp"
android:text="0"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.061"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.957" />

<Button
android:id="@+id/button_equal"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="="
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.958" />

<Button
android:id="@+id/button_multi"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="×"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button_divide"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="/"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.987"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_add"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="+"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button_sub"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="-"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button_clear"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="AC"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_para1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="%"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_para2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="+/-"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.349"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_dot"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="."
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.671"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.958" />

<TextView
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light"
android:textSize="68sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.252" />

<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />

</androidx.constraintlayout.widget.ConstraintLayout>
 
Main Activity :


import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b0;
private Button b_equal;
private Button b_multi;
private Button b_divide;
private Button b_add;
private Button b_sub;
private Button b_clear;
private Button b_dot;
private Button b_para1;
private Button b_para2;
private TextView t1;
private TextView t2;
private final char ADDITION = '+';
private final char SUBTRACTION = '-';
private final char MULTIPLICATION = '*';
private final char DIVISION = '/';
private final char EQU = '=';
private final char EXTRA = '@';
private final char MODULUS = '%';
private char ACTION;
private double val1 = Double.NaN;
private double val2;

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

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "1");
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "2");
}
});

b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "3");
}
});

b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "4");
}
});

b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "5");
}
});

b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "6");
}
});

b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "7");
}
});

b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "8");
}
});

b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "9");
}
});

b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "0");
}
});

b_dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exceedLength();
t1.setText(t1.getText().toString() + ".");
}
});

b_para1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = MODULUS;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "%");
} else {
t2.setText((int) val1 + "%");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = ADDITION;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "+");
} else {
t2.setText((int) val1 + "+");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = SUBTRACTION;
operation();
if (t1.getText().length() > 0)
if (!ifReallyDecimal()) {
t2.setText(val1 + "-");
} else {
t2.setText((int) val1 + "-");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_multi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = MULTIPLICATION;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "×");
} else {
t2.setText((int) val1 + "×");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = DIVISION;
operation();
if (ifReallyDecimal()) {
t2.setText((int) val1 + "/");
} else {
t2.setText(val1 + "/");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_para2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!t2.getText().toString().isEmpty() || !t1.getText().toString().isEmpty()) {
val1 = Double.parseDouble(t1.getText().toString());
ACTION = EXTRA;
t2.setText("-" + t1.getText().toString());
t1.setText("");
} else {
t2.setText("Error");
}
}
});

b_equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
operation();
ACTION = EQU;
if (!ifReallyDecimal()) {
t2.setText(/*t2.getText().toString() + String.valueOf(val2) + "=" + */String.valueOf(val1));
} else {
t2.setText(/*t2.getText().toString() + String.valueOf(val2) + "=" + */String.valueOf((int) val1));
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
CharSequence name = t1.getText().toString();
t1.setText(name.subSequence(0, name.length() - 1));
} else {
val1 = Double.NaN;
val2 = Double.NaN;
t1.setText("");
t2.setText("");
}
}
});

// Empty text views on long click.
b_clear.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
val1 = Double.NaN;
val2 = Double.NaN;
t1.setText("");
t2.setText("");
return true;
}
});
}

private void viewSetup() {
b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
b5 = findViewById(R.id.button5);
b6 = findViewById(R.id.button6);
b7 = findViewById(R.id.button7);
b8 = findViewById(R.id.button8);
b9 = findViewById(R.id.button9);
b0 = findViewById(R.id.button0);
b_equal = findViewById(R.id.button_equal);
b_multi = findViewById(R.id.button_multi);
b_divide = findViewById(R.id.button_divide);
b_add = findViewById(R.id.button_add);
b_sub = findViewById(R.id.button_sub);
b_clear = findViewById(R.id.button_clear);
b_dot = findViewById(R.id.button_dot);
b_para1 = findViewById(R.id.button_para1);
b_para2 = findViewById(R.id.button_para2);
t1 = findViewById(R.id.input);
t2 = findViewById(R.id.output);
}

private void operation() {
if (!Double.isNaN(val1)) {
if (t2.getText().toString().charAt(0) == '-') {
val1 = (-1) * val1;
}
val2 = Double.parseDouble(t1.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 EXTRA:
val1 = (-1) * val1;
break;
case MODULUS:
val1 = val1 % val2;
break;
case EQU:
break;
}
} else {
val1 = Double.parseDouble(t1.getText().toString());
}
}

// Remove error message that is already written there.
private void ifErrorOnOutput() {
if (t2.getText().toString().equals("Error")) {
t2.setText("");
}
}

// Whether value if a double or not
private boolean ifReallyDecimal() {
return val1 == (int) val1;
}

private void noOperation() {
String inputExpression = t2.getText().toString();
if (!inputExpression.isEmpty() && !inputExpression.equals("Error")) {
if (inputExpression.contains("-")) {
inputExpression = inputExpression.replace("-", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("+")) {
inputExpression = inputExpression.replace("+", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("/")) {
inputExpression = inputExpression.replace("/", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("%")) {
inputExpression = inputExpression.replace("%", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("×")) {
inputExpression = inputExpression.replace("×", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
}
}

// Make text small if too many digits.
private void exceedLength() {
if (t1.getText().toString().length() > 10) {
t1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
}
}

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses