𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Hello, dear Hive Learners, I hope you all are well. Today we will learn how to add two numbers. We add two numbers by using the input from the user. We use two edttext to get the input from the end-user and a button to add these two numbers and show them on the screen. we will write all the login in the on-click listener method of the button. I change the ID and text of the fields and the button in the XML file and it accordingly in the java file.

GitHub Link
Use this GitHub project to clone into your directory. It will always get updated in the next lecture. So that you will never miss the latest code. Happy Coding!.
What Should I Learn
- How to add two number
- Show the output on the screen
Assignment
- Design a layout and add two number
- Show the result on the screen by using Toast
Procedure
Here we need to change the IDs and the text to get a proper design. Here is the code of complete activity_main.xml
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hive Learners"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number">
<EditText
android:id="@+id/num1_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textColor="@color/black"
android:textColorHint="@color/purple_200"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number">
<EditText
android:id="@+id/num2_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textColor="@color/black"
android:textColorHint="@color/purple_200"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/add_btn"
style="@style/Widget.AppCompat.ImageButton"
android:layout_width="200dp"
android:layout_height="50dp"
app:icon="@drawable/hive_logo"
app:iconSize="30dp"
app:iconTintMode="multiply"
android:text="Add"
android:layout_gravity="center"/>
</LinearLayout>

Now we need to change the IDs in the JAVA file also. Here is the complete code for the MainActivity.java file. I empty the on-click listener logic to start from scratch.

Now we need to store the two numbers from the fields num1_et, num2_et to two variables by converting them to integers.
int num1 = Integer.parseInt(num1_et.getText().toString());
int num2 = Integer.parseInt(num2_et.getText().toString());

Now we need to declare a third int variable and add these two number to it and show it on the screen by using the Toast. Here we successfully add two numbers, We need to convert the result of numbers to String to show in the Toast. Because Toast is support only String, not integers or other types.
int result = num1 + num2;
Toast.makeText(MainActivity.this, String.valueOf(result), Toast.LENGTH_SHORT).show();

Now, let's run and check if the app is to working correctly?

So our app and the logic are working perfectly. We will design the calculator layout in our next lecture.

Thank You
