Android App for Adding two Number

Basic Android Application for Adding Two Integer Number.
How to Create EditText
How to Crate TextView
How to Create Button
Event Handling of Button
Access the Value of EditText

activity_main.xml

<?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"
    android:background="@color/colorPrimaryDark">

    <EditText
        android:id="@+id/etNum1"
        android:layout_width="282dp"
        android:layout_height="62dp"
        android:ems="10"
        android:inputType="number"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.11" />

    <EditText
        android:id="@+id/etNum2"
        android:layout_width="284dp"
        android:layout_height="62dp"
        android:ems="10"
        android:inputType="number"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etNum1"
        app:layout_constraintVertical_bias="0.063" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="173dp"
        android:layout_height="63dp"
        android:text="ADD"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvAns"
        app:layout_constraintVertical_bias="0.176" />

    <TextView
        android:id="@+id/tvAns"
        android:layout_width="250dp"
        android:layout_height="73dp"
        android:text="Answer"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etNum2"
        app:layout_constraintVertical_bias="0.136" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package nielit.online.add2number;

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 {

    private EditText num1;
    private EditText num2;
    private TextView ans;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        num1=findViewById(R.id.etNum1);
        num2=findViewById(R.id.etNum2);
        btn=findViewById(R.id.btnAdd);
        ans=findViewById(R.id.tvAns);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num_1=Integer.parseInt(num1.getText().toString());
                int num_2=Integer.parseInt(num2.getText().toString());
                int res=num_1+num_2;
                ans.setText(Integer.toString(res));
            }
        });
    }
}

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons