Calculating the average of two numbers in Android Studio is a simple task that can be achieved by following these steps.
Here is an example of how you can calculate the average of two numbers in Android Studio
Step 1: Create a new Android Studio project and choose “Empty Activity” as the activity template.
Step 2: In the layout file (e.g. activity_main.xml
), add two EditText
views and a Button
view. The EditText
views will be used to input the numbers and the Button
will be used to trigger the average calculation. You can also add a TextView
view to display the result.
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 | <?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" tools:context=".MainActivity"> <EditText android:id="@+id/num1EditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:ems="10" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/num2EditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:ems="10" android:inputType="number" app:layout_constraintEnd_toEndOf="@+id/num1EditText" app:layout_constraintStart_toStartOf="@+id/num1EditText" app:layout_constraintTop_toBottomOf="@+id/num1EditText" /> <TextView android:id="@+id/resultTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="Result" app:layout_constraintEnd_toEndOf="@+id/num2EditText" app:layout_constraintStart_toStartOf="@+id/num2EditText" app:layout_constraintTop_toBottomOf="@+id/num2EditText" /> <Button android:id="@+id/calculateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="28dp" android:text="Calculate" app:layout_constraintEnd_toEndOf="@+id/resultTextView" app:layout_constraintStart_toStartOf="@+id/resultTextView" app:layout_constraintTop_toBottomOf="@+id/resultTextView" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Step 3: In the activity class (e.g. MainActivity.java), define two variables to store the input numbers and a third variable to store the result.
Step 4: In the onCreate
method, set up the Button
click listener and implement the average calculation logic. To get the input numbers from the EditText
views, you can use the getText
method and convert the text to a number using the parseInt
or parseDouble
method.
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 | package com.example.averagetwonumbers; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { double num1,num2, result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText num1EditText = findViewById(R.id.num1EditText); final EditText num2EditText = findViewById(R.id.num2EditText); final Button calcButton = findViewById(R.id.calculateButton); final TextView resultTextView = findViewById(R.id.resultTextView); calcButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { num1 = Double.parseDouble(num1EditText.getText().toString()); num2 = Double.parseDouble(num2EditText.getText().toString()); result = (num1 + num2) / 2; resultTextView.setText(Double.toString(result)); } }); } } |
Output:
Additional: To make the user interface more user-friendly, you can add error handling to handle cases where the user does not enter a valid number in the EditText
views. You can do this by using the try
and catch
blocks to catch any exceptions that may be thrown when parsing the input to a number.
To text exception you must change inputType property as text in XML.
1 2 3 | android:inputType="text" |
You can also add additional functionality to the app, such as the ability to clear the input and result, or to perform other mathematical operations such as subtraction, multiplication, and division.
MainActivity.java
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 | package com.example.averagetwonumbers; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { double num1,num2, result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText num1EditText = findViewById(R.id.num1EditText); final EditText num2EditText = findViewById(R.id.num2EditText); final Button calcButton = findViewById(R.id.calculateButton); final TextView resultTextView = findViewById(R.id.resultTextView); calcButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { num1 = Double.parseDouble(num1EditText.getText().toString()); num2 = Double.parseDouble(num2EditText.getText().toString()); }catch (NumberFormatException ex){ resultTextView.setText("Error..."); return; } result = (num1 + num2) / 2; resultTextView.setText(Double.toString(result)); } }); } } |
Youtube: