Event handling training in Android programming

Event handling are useful ways to gather data on user interaction with the interactive components of applications. For example, pressing a button or touching the device’s touch screen and the like; 

It counts as an event. Android framework; Manages the event queue based on the first output of the first input (FIFO). You can record these events in your schedule and take action as needed.

Here are three concepts related to Android event management :

  • Event contacts – an event contact; An interface in the View class that includes a single callback method. When the View is triggered by the user interacting with an item in the UI, the contact is triggered; These methods are called by the Android framework.
  • Event Contact Registration  Event Registration; It is the process by which an event controller registers with an event contact in such a way that the controller is called when the event contact notifies the event.
  • Event Controllers – When an event occurs and we for it; We record an event audience; The audience of the event actually calls the event controllers, which is a method that actually examines the event.

Event contacts and event managers

Event Manager The audience of the event and its explanation

onClick ()

OnClickListener ()

This method is called when the user clicks on a widget such as a button, text, image, and so on; Or touch them. You must use onClick () to manage such events.

onLongClick ()

OnLongClickListener ()

This method is called when the user clicks (or touches a widget, such as a button, text, image, and… for a few seconds). You will use onLongClick () to manage such events.

onFocusChange ()

onFocusChangeListener ()

This method is called when the focus is removed from a widget, ie the user exits the view item. You will use the onFoocusChange () event manager to manage such events.

onkey ()

onFocusChangeListener ()

This method is called when the user focuses on an item and presses or releases a hardware key on the device. You use the onKey () event manager to manage such events.

onTouch ()

OnTouchListener ()

This method is called when the user presses or releases the key; Or make any other gestures on the screen. To manage such an event; You will use the OnTouch Event Manager ().

onMenuItemClick ()

onMenuItemClickListener ()

This method is called when the user selects a menu item. You will use onMenuItemClick () to manage such events.

onCreateContextMenu ()

onCreateContextMenuListener ()

This method is called when the context menu starts to build (resulting in a long persistent click)

Contacts There are more events available as part of the View class; Such as OnHoverListener, OnDragListener, etc. that are required for your application. Therefore, we suggest that if you create advanced applications, to develop Android applications; Refer to the official documentation.

Record event contacts

Event logging is the process by which an event manager registers with an event audience, therefore; The event manager is called when the event recipient; Notifies the event. Although there are several tricks you can do for each event; Record your event audience; But we have listed only 3 methods that you can use whichever you want based on your circumstances.

  • Use anonymous internal class
  • The Activity class implements the contact interface.
  • Use the activity_main.xml paging file to specify the event manager directly.

The following section provides detailed examples of all three of the above methods.

Touch mode

Users can use hardware keys or buttons or touch the screen; Interact with their device. Touch the screen to enter the touch mode. The user can then touch the virtual buttons on the screen; Or images and… communicate with the device. You can check if the device is in touch by calling the View class`s isINTouchMode () method.

Focus mode

A view or widget is usually entered when in focus mode; Shows or shows a blinking cursor. This indicates that it is ready to accept input from the user.

  • isFocusable () – which returns true or false statements.
  • () isFocusableInTouchMode- checks whether the view is in touch mode; Is it focusable or not. (A view may be focusable when using a hardware key, but is no longer focusable when the device is in touch).
  • android: foucsUp = ”@ = id / button_l”

OnTouchEvent ()

public boolean onTouchEvent (motionEvent event) {

switch (event.getAction ()) {

case TOUCH_DOWN:

Toast.makeText (this, ”you have clicked down Touch button”, Toast.LENTH_LONG) .show ();

break ();

case TOUCH_UP:

Toast.makeText (this, ”you have clicked up touch button”, Toast.LENTH_LONG) .show ();

break;

case TOUCH_MOVE:

Toast.makeText (this, ”you have clicked move touch button” Toast.LENTH_LONG) .show ();

break;

}

return super.onTouchEvent (event);

}

Examples of event management

Record event contacts using an anonymous internal class

Here you will create an anonymous implementation of the audience, and it will be useful if each class applies only to a single control, and you will have the advantage that you can pass arguments to the administrator. Move the event. In this way, event control methods can access private Activity data. You do not need a reference to call Activity.

But if you have applied the administrator to more than one control, you have to cut and paste the code related to the administrator and if the code for the administrator; The longer it is, the harder it is to memorize the code.

Below are simple steps to demonstrate how to use a separate Listener class to record a click event. In the same way you can implement your audience for any other required event.

the level Explanation
1 You will use the Android IDE Studio to create an Android application, and as we explained in the example chapter Hello World ; Name it myapplication under the package com.example.myapplication.
۲ Improved src / MainActivity.java file is defined for adding click event contacts and admins for two buttons.
3 Improve the res / layout / activity_main.xml file to include Android UI controls.
4 There is no need to express and define default string constants. Android Studio protects the default constants.
5 Run the program to launch the Android prototype and confirm that the results of the changes in the application.

The following is the modified content of the main activity file src / com.example.myapplcation / MainActivity.java.

 This file can contain any of the basic lifetime methods.

package com.example.myapplication;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

private ProgressDialog progress;

Button b1, b2;

@Override

protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

progress = new ProgressDialog (this);

b1 = (Button) findViewById (R.id.button);

b2 = (Button) findViewById (R.id.button2);

b1.setOnClickListener (new View.OnClickListener () {

@Override

public void onClick (View v) {

TextView txtView = (TextView) findViewById (R.id.textView);

txtView.setTextSize (25);

}

});

b2.setOnClickListener (new View.OnClickListener () {

@Override

public void onClick (View v) {

TextView txtView = (TextView) findViewById (R.id.textView);

txtView.setTextSize (55);

}

});

}

}

The following is the contents of the res / layout / activity_main.xml file:

In this section, abc actually shows the tutorialspoint logo.

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<RelativeLayout

xmlns: android = ”http://schemas.android.com/apk/res/android”

xmlns: tools = ”http://schemas.android.com/tools”

android: layout_width = ”match_parent”

android: layout_height = ”match_parent”

android: paddingBottom = ”@ dimen / activity_vertical_margin”

android: paddingLeft = ”@ dimen / activity_horizontal_margin”

android: paddingRight = ”@ dimen / activity_horizontal_margin”

android: paddingTop = ”@ dimen / activity_vertical_margin”

tools: context = ”. MainActivity”>

<TextView

android: id = ”@ + id / textView1 ″

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”Event Handling“

android: layout_alignParentTop = ”true”

android: layout_centerHorizontal = ”true”

android: textSize = ”30dp” />

<TextView

android: id = ”@ + id / textView2 ″

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”Tutorials point“

android: textColor = ”# ff87ff09 ″

android: textSize = ”30dp”

android: layout_above = ”@ + id / imageButton”

android: layout_centerHorizontal = ”true”

android: layout_marginBottom = ”40dp” />

<ImageButton

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: id = ”@ + id / imageButton”

android: src = ”@ drawable / abc”

android: layout_centerVertical = ”true”

android: layout_centerHorizontal = ”true” />

<Button

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”Small font”

android: id = ”@ + id / button”

android: layout_below = ”@ + id / imageButton”

android: layout_centerHorizontal = ”true” />

<Button

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”Large Font”

android: id = ”@ + id / button2 ″

android: layout_below = ”@ + id / button”

android: layout_alignRight = ”@ + id / button”

android: layout_alignEnd = ”@ + id / button” />

<TextView

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”Hello World!”

android: id = ”@ + id / textView”

android: layout_below = ”@ + id / button2 ″

android: layout_centerHorizontal = ”true”

android: textSize = ”25dp” />

</RelativeLayout>

The next box contains res / values ​​/ strings.xml content to define two new constants; it shows:

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<resources>

<string name = ”app_name”> myapplication </string>

</resources>

In the next section, you will see the default AndroidMinifest.xml content:

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<manifest xmlns: android = ”http://schemas.android.com/apk/res/android”

package = ”com.example.myapplication”>

<application

android: allowBackup = ”true”

android: icon = ”@ drawable / ic_launcher”

android: label = ”@ string / app_name”

android: theme = ”@ style / AppTheme”>

<activity

android: name = ”com.example.myapplication.MainActivity”

android: label = ”@ string / app_name”>

<intent-filter>

<action android: name = ”android.intent.action.MAIN” />

<category android: name = ”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

</application>

</manifest>

Let’s try our myapplication.

I assume you are in the process of setting the environment; You have created your own AVD. To run the application from Android Studio; Open one of your project activity files and click on the icon   Eclipse Run IconRun in the toolbar; click. Android Studio installs and launches the app on AVD, and if all goes well with your app and settings; The following simulator window will be displayed.

Android Event Handling

Now try to click on two buttons, one after the other. You will see that the Hello World text font will change. This is because the click-through event management method is registered; Called after each click.

Practice

We suggest that you try to code different event handlers and understand their exact differences in different types of events. Menu-related events, spinners, and body widgets are slightly different but as described above; They are written based on similar concepts.

Leave a Reply

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