This example demonstrates I’ll show you how to Write a program to create a simple calculator in Android Studio sing Java.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> <TextView android:id="@+id/textResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:background="#008080" android:padding="5dp" android:text="Code4Example" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" /> <EditText android:id="@+id/editNum1" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <EditText android:id="@+id/editNum2" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editNum1" android:layout_centerInParent="true" /> <GridLayout android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:layout_below="@+id/editNum2" android:columnCount="2" android:rowCount="2" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnAdd" android:text="+" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnSub" android:text="-" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnMul" android:text="*" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnDiv" android:text="/" /> </GridLayout> </RelativeLayout> |
Step 3 − Add the following code to src/MainActivity.java
You may also like: Android Alert Dialog Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText editNum1,editNum2; TextView textResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editNum1= findViewById(R.id.editNum1); editNum2= findViewById(R.id.editNum2); textResult= findViewById(R.id.textResult); } public void btnAdd(View view){ double num1 = Double.parseDouble(editNum1.getText().toString()); double num2 = Double.parseDouble(editNum2.getText().toString()); double result = num1 + num2; textResult.setText(Double.toString(result)); } public void btnSub(View view){ double num1 = Double.parseDouble(editNum1.getText().toString()); double num2 = Double.parseDouble(editNum2.getText().toString()); double result = num1 - num2; textResult.setText(Double.toString(result)); } public void btnMul(View view){ double num1 = Double.parseDouble(editNum1.getText().toString()); double num2 = Double.parseDouble(editNum2.getText().toString()); double result = num1 * num2; textResult.setText(Double.toString(result)); } public void btnDiv(View view){ double num1 = Double.parseDouble(editNum1.getText().toString()); double num2 = Double.parseDouble(editNum2.getText().toString()); double result = num1 / num2; textResult.setText(Double.toString(result)); } } |
Output:
This is an Android app written in Java using the Android Studio development environment. The code you provided is the MainActivity class, which serves as the entry point for the app.
The MainActivity class extends the AppCompatActivity class, which is part of the AndroidX library. This library provides compatibility with older versions of Android.
The MainActivity class declares several variables: two EditText variables editNum1 and editNum2, which are used to input numbers from the user, and a TextView variable textResult, which displays the result of the calculation.
In the onCreate method, the app sets the layout for the activity by calling setContentView(R.layout.activity_main). It then uses the findViewById() method to connect the variables to the corresponding views in the layout.
The MainActivity class also contains several methods: btnAdd, btnSub, btnMul, btnDiv, each of these methods handle the corresponding button press operation.
Each method starts by getting the input numbers from the EditText variables as strings and then parse them to double using the parseDouble() method of the Double class. Then it performs the corresponding operation on these numbers, and assigns the result to a variable called result.
Finally, it sets the text of the textResult TextView to the value of the result, converting it to a string using the toString() method of the Double class.
This app allows the user to input two numbers and then perform basic arithmetic operations on them. The results of the operations are then displayed to the user. The app is using the Android Studio development environment and the AndroidX library.
Additionally, the MainActivity class has four methods, one for each of the basic arithmetic operations: addition, subtraction, multiplication, and division. Each of these methods is linked to a corresponding button in the app’s layout file through the android:onClick attribute in the layout XML file.
When the user inputs two numbers in the EditText fields and clicks one of the buttons, the corresponding method is called and the app performs the arithmetic operation on the input numbers. The result is then displayed on the TextView field.
It’s important to note that the app is not handling any error cases such as dividing by zero or input validation, it’s just performing the basic operations on the numbers entered.
In general, this app demonstrates how to create a simple calculator app in Android, it receives user input, performs basic arithmetic operations and displays the results. It uses the Android Studio development environment and the AndroidX library.