Sunday , November 28 2021

Event Handler Implementation for only OnClick Event in Android Studio

Event Handler Implementation for only OnClick Event in Android Studio – All the Event Handling Mechanism we have seen so far can handle various types of Events.

Event Handler Implementation for only OnClick Event in Android Studio

But under this method we can only handle OnClick Event and this method has been developed exclusively for Android Apps mainly because most OnClick Event is fired by different UI Controls in any Android App. Go and we have to handle most of this event in our Android App.

In the last four methods, the Layout File and Backend Java File of our Android App did not have any link directly and all the Event Handler Methods and Event Source Controls were completely independent of each other.

But under this method we have to write some code in both the files, through which it is decided which event handler will respond to the events of which event source and to bind the event source and event handler, we need android: onClick Attribute has to be used which accepts the name of an Event Handler Method as a value.

For example if we want to setup a Callback Event Handler named clickHandler for OnClick Event of Button Control with R.id.btnAdd Layout ID of our Simple Calculator App, first of all we need to have R.id.btnAdd Layout ID in our Layout File Specify android: onClick = “clickHandler” Attribute in the Button Control, and then implement a Callback Method named clickHandler () in your Backend Java File which will execute when the Button with R.id.btnAdd Layout ID is clicked. .

This method of Event Handing is quite easy and different types of Events are handled in WPF (XAML) of .NET and Java’s JavaFX based GUI Applications in the same way.

But under Android, we can only handle OnClick Event in this way and to handle any event other than OnClick, we have to use any one or more of the four methods that were used in the previous Sections Discussed in detail.

If we recall our Simple Calculator Example using this method, then our modified example will be as follows:

File Name:  strings.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 Name:  activity_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:inputType="numberDecimal"
         android:text="0"
         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:inputType="numberDecimal"
         android:text="0"
         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"
         android:onClick="clickHandler" />
 
     <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"
         android:onClick="clickHandler" />
 
     <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"
         android:onClick="clickHandler" />
 
     <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"
         android:onClick="clickHandler" />
 
     <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:onClick="clickHandler" />
 </android.support.constraint.ConstraintLayout>

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{
     EditText etFN;
     EditText etSN;
     TextView tvResult;
     double fn;
     double sn;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
 
     public void clickHandler(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());
 
         switch (btnClicked.getId()) {
             case R.id.btnAdd:
                 tvResult.setText(String.valueOf(fn + sn));
                 break;
 
             case R.id.btnSub:
                 tvResult.setText(String.valueOf(fn - sn));
                 break;
 
             case R.id.btnMul:
                 tvResult.setText(String.valueOf(fn * sn));
                 break;
 
             case R.id.btnDiv:
                 tvResult.setText(String.valueOf(fn / sn));
                 break;
 
             case R.id.btnRem:
                 tvResult.setText(String.valueOf(fn % sn));
                 break;
 
             default:
                 // Nothing to do
         }
     }
 }

When we run the Android App based on this Modified Event Handler Mechanism, we still get the same output as the other Event Handling Mechanism discussed in previous Sections.

In this way we discussed a total of 5 Event Handling Mechanisms in this chapter which allow the same need to be fulfilled in 5 different ways. Now, which way you should use to fulfill the kind of need, it completely depends on you. But even if you only have to handle the OnClick Event, the fifth method is the easiest.

Since Java is a very dynamic type of Programming Language, we can only use the total of 5 methods discussed above for Event Handling, it is not that but we can also create new methods according to our needs.

Event Handler Implementation for only OnClick Event in Android Studio

But we should always keep our codes easy to Understand and Generic, so that whenever the need arises in future, it can be easily Modified or Extended to Improve and Extend Functionalities of your application and the above mentioned five methods discussed above are quite common.

So as far as possible, you should always do your Android App Event Handling using one or more of these methods.

In all the examples we have created so far, only the onClick () Callback method has been implemented in all of them, which does not return any value. But some other Event Listener Methods do a Boolean Value Return. Now whether a callback method will do a Boolean Value Return or not, it depends on some of the following reasons:

  • onLongClick() and onKey() Callback
  • onTouch() Callback

Read Also – Event Handler Implementation using Anonymous Inner Class 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 *