Thursday, 5 September 2019

Android App for Addition of Two Numbers

  
 
               In this post, we will see How to create Android App for Addition of Two Numbers. 



                How to create Android First App Hello World using Android Studio, check in following link:
https://www.comrevo.com/2019/09/Android-First-App-Hello-World-using-Android-Studio.html 
 
                To know how to install Android Studio in Linux, check following link:
https://www.comrevo.com/2019/01/how-to-install-android-studio-in-linux.html.

                To find details about Architecture of Android Operating System, check following link:
https://www.comrevo.com/2018/10/architecture-of-android-operating-system.html.


Program (activity_main.xml)   

Design Mode:   
                   Add TextView, Button, EditText (Plain Text) as shown in following screenshot. Also edit their attributes as per your requirements. 
 


Text Mode: 
                    Text Mode will get automatically updated. If you want you can update it.


<?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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="57dp"
        android:layout_marginTop="16dp"
        android:text="First Number"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="17dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toEndOf="@+id/textView1"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="57dp"
        android:layout_marginTop="49dp"
        android:text="Second Number"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="37dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/editText1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="124dp"
        android:layout_marginTop="48dp"
        android:text="ADD"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="124dp"
        android:layout_marginTop="60dp"
        android:text="Result"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button1" />
</androidx.constraintlayout.widget.ConstraintLayout>

Program (MainActivity.java)
package com.example.twonumbersaddapp;

/* We have to import AppCompatActivity class as we need to extend class AppCompatActivity which provides methods to manage Life Cycle of Activity e.g. onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy(). 

It is similar to the Applet class which is needed for creating Applet */

import androidx.appcompat.app.AppCompatActivity;

/* We need to import Bundle class as we have to pass object of Bundle class as a parameter to the onCreate() method */ 


import android.os.Bundle;

/* We need to import View class as we have to use findViewById() method which is provided by View class */  

import android.view.View;

/* GUI components Button, EditText (TextField), TextView (Label) are provided by respective classes */ 


import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText number1;
    EditText number2;
    Button Add_button;
    TextView result;
    int ans=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


/* Calling onCreate() method of super class */
        super.onCreate(savedInstanceState);


/* R is a class and layout is inner class which is defined inside R class. These classes are created during building process. activity_main is a static integer which represents activity_main.xml file. setContentView() is a method of Activity class. Activity is super class of AppCompatActivity class. setContentView() method is used to incorporate GUI components (mentioned in activity_main.xml file) into activity (frame) of our app */

        setContentView(R.layout.activity_main);
 

/* findViewById() is a method of View class. It returns object of View class. View is super class of EditText, Button, TextView. Hence we can typecast object of View class to EditText, Button, TextView */

        number1=(EditText) findViewById(R.id.editText1);
        number2=(EditText) findViewById(R.id.editText2);
        Add_button=(Button) findViewById(R.id.button1);
        result = (TextView) findViewById(R.id.textView3);
 

/* OnClickListener is an interface like ActionListener. setOnClickListener() is a method similar to the actionListener() method. onClick() method is similar to the actionPerformed() method. onClick() method accepts object of View class. */

        // Add_button add clicklistener
        Add_button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        // Declare variables num1 and num2 as double type
        // get data which is in edittext, convert it to string
        // using parseDouble() method to convert String to Double type


/* number1.getText() returns object of Editable class. Again, we need to call toString() method to get text (String) */

        double num1 = Double.parseDouble(number1.getText().toString());
        double num2 = Double.parseDouble(number2.getText().toString());
        // add both number and store it to sum
        double sum = num1 + num2;
        // set it to result textview
        result.setText(""+sum);
    } } );

}}
  
  
Output:  
                 Screenshot of the this app from mobile phone is as follows:





No comments:

Post a comment