Sunday , November 28 2021

Event Handler Implementation using Anonymous Inner Class in Android Studio

Event Handler Implementation using Anonymous Inner Class in Android Studio – Event Handler Implementation using Anonymous Inner Class – This Event Handling Mechanism is largely a mixture of both the previous methods.

Event Handler Implementation using Anonymous Inner Class in Android Studio

This method of event handling proves to be quite useful when there is no functionality in each listener that can be shared by different Event Source.

It is a little difficult for new programmers to understand the Event Handler setup in this way, but in reality this method is not at all difficult. Because this method is made up of a mixture of both Member Class and Interface Type.

In this way, by registering Event Source separately from Event Listener to Define Event Listener Class, Event Source is not registered with Event Listener through its new Object, but a new Anonymous Event Listener like Interface Type way. By creating a description of the class, the Event Source is registered with a new instance of it.

File Name:  MainActivity.java

package com.dmtechnolab.arithmetic;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.TextView;
 
 public class MainActivity extends Activity {
     EditText etFN;
     EditText etSN;
     TextView tvResult;
     double fn;
     double sn;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         //Register Event Source with Anonymous View.OnClickListener Class
         findViewById(R.id.btnAdd).setOnClickListener( new View.OnClickListener() {
             public void onClick(View btnClicked) {
                 etFN = (EditText) findViewById(R.id.etFN);
                 etSN = (EditText) findViewById(R.id.etSN);
                 tvResult = (TextView) findViewById(R.id.tvResult);
 
                 fn = Double.parseDouble(etFN.getText().toString());
                 sn = Double.parseDouble(etSN.getText().toString());
 
                 tvResult.setText(String.valueOf(fn + sn));
             }
         });
 
         findViewById(R.id.btnSub).setOnClickListener( new View.OnClickListener() {
             public void onClick(View btnClicked) {
                 etFN = (EditText) findViewById(R.id.etFN);
                 etSN = (EditText) findViewById(R.id.etSN);
                 tvResult = (TextView) findViewById(R.id.tvResult);
 
                 fn = Double.parseDouble(etFN.getText().toString());
                 sn = Double.parseDouble(etSN.getText().toString());
 
                 tvResult.setText(String.valueOf(fn - sn));
             }
         });
 
         findViewById(R.id.btnMul).setOnClickListener( new View.OnClickListener() {
             public void onClick(View btnClicked) {
                 etFN = (EditText) findViewById(R.id.etFN);
                 etSN = (EditText) findViewById(R.id.etSN);
                 tvResult = (TextView) findViewById(R.id.tvResult);
 
                 fn = Double.parseDouble(etFN.getText().toString());
                 sn = Double.parseDouble(etSN.getText().toString());
 
                 tvResult.setText(String.valueOf(fn * sn));
             }
         });
 
         findViewById(R.id.btnDiv).setOnClickListener( new View.OnClickListener() {
             public void onClick(View btnClicked) {
                 etFN = (EditText) findViewById(R.id.etFN);
                 etSN = (EditText) findViewById(R.id.etSN);
                 tvResult = (TextView) findViewById(R.id.tvResult);
 
                 fn = Double.parseDouble(etFN.getText().toString());
                 sn = Double.parseDouble(etSN.getText().toString());
 
                 tvResult.setText(String.valueOf(fn / sn));
             }
         });
 
         findViewById(R.id.btnRem).setOnClickListener( new View.OnClickListener() {
             public void onClick(View btnClicked) {
                 etFN = (EditText) findViewById(R.id.etFN);
                 etSN = (EditText) findViewById(R.id.etSN);
                 tvResult = (TextView) findViewById(R.id.tvResult);
 
                 fn = Double.parseDouble(etFN.getText().toString());
                 sn = Double.parseDouble(etSN.getText().toString());
 
                 tvResult.setText(String.valueOf(fn % sn));
             }
         });
     }
 }

Event Handler Implementation using Anonymous Inner Class in Android Studio

Even on running Android App with this modified Java file, we get Exactly the same output as was found in both the previous Examples. The only difference is that instead of defining the Event Handler Listener Class of this example separately and binding the Event Source to the Event Listener through its Object, we immediately specified the Event Handler Listener Class as an Anonymous Class and Create an Instance and register the Event Source with the Listener Class through the same Instance.

Read also: Event Handler Implementation using Member Class
Since here we are defining an Anonymous Even Listener Class, as soon as the Instance Bind of the Anonymous Event Listener Class is done with Event Source, the Event Listener Class becomes Inaccessible, resulting in Automatic Garbage Collector of Java by Destroying that Anonymous Class Free his resources.

That is why Anonymous Event Handler Listener Class has been specified separately for each Button to handle a Click Event that is fired and we can see that there are a lot of Common Program Logic Codes Repetition in this way of Event Handling. It has been not only increasing the size of the program but also has a negative effect on the performance of the program.

So it is useful to use this method only if many UI View Controls do not need to execute the same type of Codes to perform the same type of Operation but mostly in Response of Events fired by Event Source. To Perform Operations.

Read Also – How to Free RAM on Android device – 5 Best ways

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 *