My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

DialogBox Listeners


<Button
 android:id="@+id/btn_dialog"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Show Me" />

File : MainActivity.java

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class EventListeners extends Activity{
	Button btn_dialog;
	AlertDialog.Builder builder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_listener);
		
		btn_dialog=(Button)findViewById(R.id.btn_dialog);
		
		btn_dialog.setOnClickListener(btn_listener_obj);
		
		builder = new AlertDialog.Builder(this);
		builder.setTitle("This my title");
		builder.setPositiveButton("Yes", btn_pos_click);
		builder.setNegativeButton("No", btn_neg_click);
		
	}
	
	View.OnClickListener btn_listener_obj=new View.OnClickListener(){
		@Override
		public void onClick(View v) {
			builder.show();
		}
	};
	
	DialogInterface.OnClickListener btn_pos_click=new DialogInterface.OnClickListener(){

		@Override
		public void onClick(DialogInterface dialog, int which) {
			
			Toast.makeText(EventListeners.this, "Pos :"+"   "+which+"   "+DialogInterface.BUTTON_POSITIVE, Toast.LENGTH_LONG).show();
		}
	};
	
	DialogInterface.OnClickListener btn_neg_click=new DialogInterface.OnClickListener(){

		@Override
		public void onClick(DialogInterface dialog, int which) {
			Toast.makeText(EventListeners.this, "Neg :"+"   "+which+"   "+DialogInterface.BUTTON_NEGATIVE, Toast.LENGTH_LONG).show();
		}
	};
}

Button Listeners

Method 1 :


<Button
 android:id="@+id/btn_dialog"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Show Me" />

File : MainActivity.java

package com.example.androidcollegeppt;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Ex1 extends Activity{
	Button btn_dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_listener);
		btn_dialog=(Button)findViewById(R.id.btn_dialog);

		btn_dialog.setOnClickListener(btn_listener_obj);
	}
	

	View.OnClickListener btn_listener_obj=new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			Toast.makeText(Ex1.this, "Hai", Toast.LENGTH_SHORT).show();
		}

	};
}


Method 2 :

However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:

File : main_activity.xml


<Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="f1" />

Now, when a user clicks the button, the Android system calls the activity's f1(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

File : MainActivity.java


public void f1(View view) {
     // write your code here...
 }



Events, Listener, and Handling

  • In programming lang., an event is when something special happens.(click,touch,text enter, keypressed,long pressed)
  • Listener is responsible for generating response to an event.
  • Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs.

Example

Let's talk once about event processing mechanism by button click example. A button cannot process clicks on its own, it needs a listener for this, which is assigned using setOnClickListener method. When a button is clicked, listener reacts and runs the code from onClick method. It can be displayed like this:


It means that to implement this you have to follow these steps:
  1. Step 1 : create a listener
  2. Step 2 : implement onClick method
  3. Step 3 : assign listener to a button
Creating Listener, and Handing events using onClick() method

 OnClickListener btnSubmitListener= new OnClickListener() {//Listener //Step - 1
       @Override
       public void onClick(View v) {//Step - 2
 
       //Your code Here

       }
     };

Assign listener to a button :

btnCancel.setOnClickListener(btnSubmitListener);//Step - 3

The Android framework maintains an event queue.
There are following three concepts related to Android Event Management.
  1. Event Listeners :
  2. Event Listeners Registration :
  3. Event Handlers :
Event Listeners : An event listener is an interface in the View class that contains a single callback method.These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction.
Event Listeners Registration
Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event.
  1. Using an Anonymous Inner Class
  2. Activity class implements the Listener interface.
  3. Using Layout file activity_main.xml to specify event handler directly.
TOUCH EVENT :
Focus :
A view or widget is usually highlighted or displays a flashing cursor when it#39;s in focus. This indicates that it#39;s ready to accept input from the user.
Event Listener Event Handler Description
1. OnClickListener() onClick() This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc.
2. OnLongClickListener() onLongClick() This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds.
3. OnFocusChangeListener() onFocusChange() This is called when the widget looses its focus ie. user goes away from the view item.
4. OnFocusChangeListener() onKey() This is called when the user is focused on the item and presses or releases a hardware key on the device.
5. OnTouchListener() onTouch() This is called when the user presses the key, releases the key, or any movement gesture on the screen.
6. OnMenuItemClickListener() onMenuItemClick() This is called when the user selects a menu item.
7. onCreateContextMenuItemListener() onCreateContextMenu() This is called when the context menu is being built(long click)
Users can interact with their devices by using hardware keys or buttons or touching the screen.Touching the screen puts the device into touch mode.