Android App Development | Beginner Course | Lecture#13 | Hive Learners

Words
322
Reading
2 min
Listen
Play
4y

饾摉饾摶饾摦饾摦饾摻饾摬饾摲饾摪饾摷

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.

multi_purpose_3_

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>

image

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.

image

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());

image

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();

image

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

image

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

hl_divider.png

Thank You

hl_footer_banner.png

Android App Development | Beginner Course | Lecture#13 | Hive Learn... | Ecency