Android Android Java

Addition, Subtraction, Multiplication and Division Program in Android using Java5 min read

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.

Step 3 âˆ’ Add the following code to src/MainActivity.java

You may also like: Android Alert Dialog Example

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.

Leave a Comment