Getting you know about the functioning and the related code how to create a “Calculator Application” using the Android Studio platform.
Let’s get started with our calculator android App:
Step-1
Open your Android Studio Platform
Click on File Menu and start your New Android Studio Project.
Give your Application Name calcDemo and leave other fields blank as it is, then clicks NEXT.
Step-2
Select the form factors & minimum SDK API 14: Android 4.0(IceCreamSandwich). I selected API 14 (IceCreamSandwich) because by targeting API 14 & later, Your app will run on approximately 100% of devices.
Step-3
Add Empty activity to your application and click on NEXT button.
Step-4
After clicking Finish, it takes around 1 minutes to build Activity and files.
Step-5
Now we have to add our UI in our content_main.xml file.
<?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:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.msclient009.demo.MainActivity" tools:showIn="@layout/activity_main"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#b4b4b4"> <TextView android:id="@+id/txtExp" android:layout_width="match_parent" android:layout_height="wrap_content" android:height="50dp" android:text="" android:layout_marginEnd="5dp" android:gravity="end" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_marginEnd="5dp" android:id="@+id/txtResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:height="50dp" android:text="0" android:gravity="end" android:textColor="#000000" android:textSize="30sp" /> </LinearLayout> <TableLayout android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent"> <TableRow> <Button android:textSize="18sp" android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> <Button android:textSize="18sp" android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" /> <Button android:textSize="18sp" android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" /> <Button android:textSize="18sp" android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" /> </TableRow> <TableRow> <Button android:textSize="18sp" android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" /> <Button android:textSize="18sp" android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" /> <Button android:textSize="18sp" android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" /> <Button android:textSize="18sp" android:id="@+id/btnSub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" /> </TableRow> <TableRow> <Button android:textSize="18sp" android:id="@+id/btn7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" /> <Button android:textSize="18sp" android:id="@+id/btn8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" /> <Button android:textSize="18sp" android:id="@+id/btn9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" /> <Button android:textSize="18sp" android:id="@+id/btnMul" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" /> </TableRow> <TableRow> <Button android:textSize="18sp" android:id="@+id/btnDot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." android:textStyle="bold" /> <Button android:textSize="18sp" android:id="@+id/btn0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" /> <Button android:textSize="18sp" android:id="@+id/btnDiv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" /> <Button android:textSize="18sp" android:id="@+id/btnEqual" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" /> </TableRow> <TableRow> <Button android:textSize="16sp" android:id="@+id/btnBackspace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Backspace" /> <Button android:textSize="16sp" android:id="@+id/btnClear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" /> </TableRow> </TableLayout> </LinearLayout>
Step-6
After that, you can write the code in MainActivity.java file.
The following code is used to reset all values.
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtResult.setText("0");
txtExp.setText("");
num1 = "";
num2 = "";
Result = "";
symbol = 0;
number = 0;
}
});
Below code is used to remove the single character at a time in the text field.
btnBackspace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtResult.setText(txtResult.getText().toString().substring(0, txtResult.getText().length() - 1));
if (txtResult.getText().equals(""))
txtResult.setText("0");
}
});
Below code is used to evaluate the expression.
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (symbol >=1) {
if(num2=="") {
num2=num1;
}
switch (s) {
case '+':
Result = String.valueOf(Float.valueOf(num1) + Float.valueOf(num2));
break;
case '-':
Result = String.valueOf(Float.valueOf(num1) - Float.valueOf(num2));
break;
case '*':
Result = String.valueOf(Float.valueOf(num1) * Float.valueOf(num2));
break;
case '/':
Result = String.valueOf(Float.valueOf(num1) / Float.valueOf(num2));
break;
} // switch close
}
txtExp.setText("");
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
txtResult.setText(format.format(Float.valueOf(Result)));
txtExp.setText("");
num1 = "";
num2 = "";
Result = "";
symbol = 0;
number = 0;
}
});
The following code is the main code of mainActivity.java file. It can check all types of validation that are used to apply any calculator application. And we can use a different pre-defined function to evaluate an expression and check different types of validation.
@Override
public void onClick(View v) {
btnNumber = v.findViewById(v.getId());
currentText = btnNumber.getText().toString();
prevText = txtResult.getText().toString();
String lastSymbol = prevText.substring(prevText.length() - 1, prevText.length());
expPrevText = txtExp.getText().toString();
/* check the symbol if more than one times click in same place */
if(expPrevText.length()>0 && num2.equals("") && (currentText.equals("+") || currentText.equals("-") || currentText.equals("*") || currentText.equals("/") || currentText.equals("."))) {
expLastSymbol = expPrevText.substring(expPrevText.length() - 1, expPrevText.length());
txtExp.setText(expPrevText.substring(0, expPrevText.length() - 1) + currentText.toString());
if(symbol==1)
s = currentText.charAt(0);
return;
}
/* This code is run only first time when EditText value is only zero */
if (prevText.equals("0")) {
if (currentText.equals("+") || currentText.equals("-") || currentText.equals("*") || currentText.equals("/") || currentText.equals(".")) {
txtResult.setText(prevText.concat(currentText));
if (!currentText.equals("."))
symbol++;
} else
txtResult.setText(currentText);
} else {
/* validation code (. and +,-,*,/) */
if (prevText.indexOf(".")>0 && currentText.equals("."))
return;
if (prevText.indexOf(".")<0 && currentText.equals("."))
{
txtResult.setText(prevText.concat(currentText));
return;
}
if (currentText.equals(".") && lastSymbol.equals("."))
txtResult.setText(prevText.substring(0, prevText.length() - 1) + currentText.toString());
expPrevText = txtExp.getText().toString();
/* if we click any operands & dot symbol then below condition is true */
if ((TextUtils.isDigitsOnly(currentText) && (Integer.valueOf(currentText) >= 0 && Integer.valueOf(currentText) <= 9)) || currentText.equals(".")) {
expLastSymbol="";
if (number < 1)
txtResult.setText(prevText.concat(currentText));
if (symbol >= 1 && number == 1) {
txtResult.setText("");
txtResult.setText(currentText);
}
number = 0;
/* If we get symbol greater or equals to one & number equals to zero then Initialized the Second operands value */
if (symbol >= 1 && number == 0)
num2 = txtResult.getText().toString();
}
/* When we Click any operator (Like +,_ ,*,/, .) then below code is run */
if (currentText.equals("+") || currentText.equals("-") || currentText.equals("*") || currentText.equals("/") || currentText.equals(".")) {
number++;
symbol++;
if (symbol == 1) {
txtExp.setText(prevText.concat(currentText));
s = currentText.charAt(0);
} else {
expPrevText = txtExp.getText().toString();
txtExp.setText(expPrevText.concat(prevText));
//txtExp.setText(txtExp.concat(currentText));
txtExp.setText(txtExp.getText().toString().concat(currentText));
}
/* If we get symbol equals to one then Initialized the first operands value */
if (symbol == 1)
num1 = prevText.toString();
if (symbol >= 2) {
/* Below code is used to perform ( +,-,*, / ) operation. */
switch (s) {
case '+':
Result = String.valueOf(Float.valueOf(num1) + Float.valueOf(num2));
break;
case '-':
Result = String.valueOf(Float.valueOf(num1) - Float.valueOf(num2));
break;
case '*':
Result = String.valueOf(Float.valueOf(num1) * Float.valueOf(num2));
break;
case '/':
Result = String.valueOf(Float.valueOf(num1) / Float.valueOf(num2));
break;
} // switch close
/* It while convert the value to real number if all value after decimal point is zero */
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
txtResult.setText(format.format(Float.valueOf(Result)));
num1=Result.toString();
s=currentText.charAt(0);
num2="";
symbol=1;
number=1;
Result="0";
} // if(symbol >=2) end
} // +,-,*,/ close
} // else close
} // onClick close
The Output of above code:
Leave Comment