Sunday , November 28 2021

Event Handler Implementation using Member Class in Android Studio

Event Handler Implementation using Member Class in Android Studio – Android SDK defines a different Listener Interface to listen to all types of Events.

That can fire an Event, and each View UI Control To determine the types of Events you can listen to, you can get detailed information about different types of Listener Interfaces from the Interfaces section of the android.view Package on the Android Developer Site where normally each Listener Interfacer has the name “Listener Word is included.

Event Handler Implementation using Member Class in Android Studio

For example when a View is clicked, View.OnClickListener is Event Fire, when a View is kept pressed for a long time, then View.OnLongClickListener is Event Fire, when a View is scrolled. ,

Then View.OnScrollChangeListener is Event Fire and as you can see all of these have Listener Word included so that you can easily identify which type of event to handle which is related to meeting the need. Which Listener Interface to Implement.

Since, in our Current Example, we have to add two numbers in the Response to Click on Add Button, so to handle Button’s Click Event, we have to implement the View.OnClickListener Interface and name onClick () in this listener.

Has only one Abstract Method, which has to be defined as an Event Handler as it is the Callback Method, which will execute when clicked on the UI View Control, which is Associated with its Listener Interface.

Java Programming Language provides us the facility to implement an Interface in many ways and under the first of these methods we create a Private Member Class within the Activity Class and Implement the Listener Interface in the same Member Class. Let’s take it.

This method is commonly used when many listeners have to do the same type of processing and it is possible to handle all the processings by a single class so that more and more operations are performed by at least Program Codes. To go

For example, if we take an example of a simple calculator that has 5 Button Controls related to Addition, Subtraction, Multiplication, Division and Reminder Return, only five Buttons do Result Return by executing similar Program Codes. That is, if we want to combine two numbers, result = firstNumber + secondNumber can execute the Expression whereas if we want to multiply two numbers, we can write result = firstNumber * secondNumber to the same expression.

In order to generate the results that are returned by the execution of similar program logics to such a large extent, we can use the same Event Listener Class to handle similar events fired by multiple UI Controls in general. And the best way to do this is to define the Event Listener Class as a Member Class or Nested Class of the relevant UI Activity Class and execute the onClick () Callback Method of the same Event Listener Class for all Buttons. .

Then in this onClick () Callback Method, find out which button is clicked and according to the same button, addition, Subtraction, Multiplication, Division or Reminder will execute the expression and display the result on the Output Screen. The whole example based on this process is as follows –

File Namestrings.xml

<resources>
 <string name="app_name">Simple Calculator</string>
 <string name="fn">First Number</string>
 <string name="sn">Second Number</string>
 <string name="result">Result</string>

 <string name="add">+</string>
 <string name="sub">-</string>
 <string name="mul">x</string>
 <string name="div">/</string>
 <string name="Rem">%</string>

 <dimen name="layoutPadding">5dp</dimen>
</resources>

File Nameactivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.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="wrap_content"
 android:padding="@dimen/layoutPadding"
 tools:context="com.bccfalna.arithmetic.MainActivity">

<TextView
 android:id="@+id/tvFN"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/sn"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintTop_toTopOf="@+id/etFN"
 android:layout_marginTop="22dp" />

<TextView
 android:id="@+id/tvSN"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginTop="25dp"
 android:text="@string/fn"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/tvFN" />

<EditText
 android:id="@+id/etFN"
 android:layout_width="0dp"
 android:layout_height="39dp"
 android:layout_marginLeft="40dp"
 android:layout_marginStart="40dp"
 android:layout_marginTop="8dp"
 android:ems="10"
 android:text="0"
 android:inputType="numberDecimal"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintLeft_toRightOf="@+id/tvFN"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

<TextView
 android:id="@+id/tvRes"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginTop="18dp"
 android:text="@string/result"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/tvSN" />

<EditText
 android:id="@+id/etSN"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:ems="10"
 android:text="0"
 android:inputType="numberDecimal"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintLeft_toLeftOf="@+id/etFN"
 app:layout_constraintRight_toRightOf="@+id/etFN"
 app:layout_constraintTop_toBottomOf="@+id/etFN" />

<TextView
 android:id="@+id/tvResult"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:textSize="25sp"
 android:textStyle="bold"
 app:layout_constraintLeft_toLeftOf="@+id/etSN"
 app:layout_constraintRight_toRightOf="@+id/etFN"
 app:layout_constraintTop_toBottomOf="@+id/etSN"
 app:layout_constraintHorizontal_bias="0.0" />

<Button
 android:id="@+id/btnAdd"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/add"
 app:layout_constraintRight_toLeftOf="@+id/btnSub"
 app:layout_constraintTop_toTopOf="@+id/btnSub" />

<Button
 android:id="@+id/btnSub"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/sub"
 app:layout_constraintRight_toLeftOf="@+id/btnMul"
 app:layout_constraintTop_toTopOf="@+id/btnMul" />

<Button
 android:id="@+id/btnMul"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/mul"
 app:layout_constraintRight_toLeftOf="@+id/btnDiv"
 app:layout_constraintTop_toTopOf="@+id/btnDiv" />

<Button
 android:id="@+id/btnDiv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/div"
 app:layout_constraintTop_toTopOf="@+id/btnRem"
 app:layout_constraintRight_toLeftOf="@+id/btnRem" />

<Button
 android:id="@+id/btnRem"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/Rem"
 app:layout_constraintTop_toBottomOf="@+id/tvRes"
 android:layout_marginTop="5dp"
 app:layout_constraintRight_toRightOf="parent" />
 </android.support.constraint.ConstraintLayout>

Event Handler Implementation using Member Class in Android Studio

File Name:  MainActivity.java

package com.bccfalna.arithmetic;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.TextView;
 
 public class MainActivity extends Activity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         //Attach an Object of ClickHandler Listener Class to each Button
         findViewById(R.id.btnAdd).setOnClickListener(new ClickHandler());
         findViewById(R.id.btnSub).setOnClickListener(new ClickHandler());
         findViewById(R.id.btnMul).setOnClickListener(new ClickHandler());
         findViewById(R.id.btnDiv).setOnClickListener(new ClickHandler());
         findViewById(R.id.btnRem).setOnClickListener(new ClickHandler());
     }
 
     private class ClickHandler implements View.OnClickListener {
         public void onClick(View btnClicked) {
             // Get a reference to the etFN EditText of Layout
             EditText etFN = (EditText) findViewById(R.id.etFN);
 
             // Get a reference to the etSN EditText of Layout
             EditText etSN = (EditText) findViewById(R.id.etSN);
 
             // Get a reference to the tvResult TextView of Layout
             TextView tvResult = (TextView) findViewById(R.id.tvResult);
 
             // Type Cast etFN and etSN Text into String and then into Value
             double fn = Double.parseDouble(etFN.getText().toString());
             double sn = Double.parseDouble(etSN.getText().toString());
 
             switch (btnClicked.getId()) {
                 case R.id.btnAdd:
                     // tvResult = etFN + etSN
                     tvResult.setText(String.valueOf(fn + sn));
                     break;
 
                 case R.id.btnSub:
                     // tvResult = etFN - etSN
                     tvResult.setText(String.valueOf(fn - sn));
                     break;
 
                 case R.id.btnMul:
                     // tvResult = etFN x etSN
                     tvResult.setText(String.valueOf(fn * sn));
                     break;
 
                 case R.id.btnDiv:
                     // tvResult = etFN / etSN
                     tvResult.setText(String.valueOf(fn / sn));
                     break;
 
                 case R.id.btnRem:
                     // tvResult = etFN % etSN
                     tvResult.setText(String.valueOf(fn % sn));
                     break;
                 default:
                     // Nothing to specify
             }
         }
     }
 }

When we run this example, we get the Output Layout as shown in the next figure and if you have understood the previous chapter properly, then you are sure to understand the XML codes written in activity_main.xml and string.xml file. There will be no problem in form.

So now we will understand the Program Logic i.e. Java Codes written directly in the ActivityMain.java File.

To handle Event handling in any Java GUI program we need three things –

The UI View Object that Event Fire does.
The object of the Event Listener Class that listens to the event that is fired, which is notify from the event that is fired and
Callback Event Handler Method, which responds to an event that fires through the Notify Event Listener Object.
Now if we identify these three according to our example, then-

Buttons that appear on Simple Calculator are UI View Controls that fire an Event named Click.

As soon as a Button does a Click Event Fire, some Event Listener Class is defined to capture it and in our example the name of that Listener Class is HandleClick because in this class we have defined the layout of our Current Activity. In order to capture any type of click event that happens to be fire, Interface named OnClickListener has been implemented.

But no Callback Event Handler Method can be executed unless there is an interconnection between the UI View Control that fires the Event and the Event Listener Object that listens to the Event. Therefore, the work of interconnecting these two is done by the View.setOn… Listener () method.

Which Event Listener will respond to events fired by which UI View Control, this is decided by the View.setOn… Listener () method and this process of attaching Event Listener with UI View Control is done by UI View Control. It is also called registering.

If we say the same based on our example, which event listener captures a click event fired by Kiss Button and will respond by its Callback Event Handler Method, we need to name ClickHandler by onSetClickListener () method. Button Controls that fire an Event with the Listener Object of K have to register.

That is, we have to tell the Android system running Android App that the click events fired by the five Button Controls of our Current Example are to be Handle or Response by the Implemented Callback Event Handler Method in the ClickHandler Listener Class and Android To tell the system this, we have registered all the Button Controls by the following Statements with a new Listener Object of the ClickHandler Class that establishes an interconnection between the Listner Class and the Button Controls that fire the Event:

findViewById(R.id.btnAdd).setOnClickListener(new ClickHandler());
findViewById(R.id.btnSub).setOnClickListener(new ClickHandler());
findViewById(R.id.btnMul).setOnClickListener(new ClickHandler());
findViewById(R.id.btnDiv).setOnClickListener(new ClickHandler());
findViewById(R.id.btnRem).setOnClickListener(new ClickHandler());

If you look at the first statement, this statement tells the Underlying Android System that the UI View Control ID in the Layout of Current Activity is btnAdd, Handling those Click Events Fire by a class named ClickHandler. Will go.

Similarly, under another statement, the Underlying Android System has been told that the UI View Control ID of btnSub in the Layout of Current Activity will be handled by a class named ClickHandler to handle any Click Events Fire. .

Similarly all other Buttons have also been registered with ClickHandler Class so that Click Events fired by all Buttons are responded to by ClickHandler Class’s Callback Event Handler Method.

When we can bind or register a UI View Control with its Event Listener Class as above, then the Callback Event Handler Method of its Registrar Listener Class gets executed as soon as that UI View Control fires an Event. And the Fired Event is handled.

Read Also – Event Handler Implementation for only OnClick Event in Android Studio

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

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