Studyon Minte9.com
Android




Input Events



The example below shows how to register an on-click listener for a Button. 
	
	* onClick()
	* onLongClick()
	* onFocusChange() - called when the user navigates onto or away from the item
	* onKey() - called when the user is focused on the item and presses a key
	* onTouch() - a press, a release, etc (within the bounds of the item)
	* onCreateContextMenu() - called when a Context Menu is being built (result of a "long click")

	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content">
	    
	    <Button 
		android:id="@+id/my_button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="my click"
		/>

	</LinearLayout>

	%--- MainActivity.java

	package com.example.myapp;
	import android.os.Bundle;
	import android.support.v4.app.FragmentActivity;
	import android.view.View;
	import android.view.View.OnClickListener;
	import android.widget.Button;
	import android.widget.Toast;

	public class MainActivity extends FragmentActivity {
			
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button button = (Button) findViewById(R.id.my_button);
		button.setOnClickListener(myListener);
	    }

	    /*--- LOOK HERE ---*/
	    
	    private OnClickListener myListener = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			
			Toast.makeText(getApplicationContext(), "My Message", Toast.LENGTH_SHORT).show();
		}
	    };
	    
	}

	
Notice that the onClick() callback in the above example has no return value, but some other event listener 
methods must return a boolean.


http://developer.android.com/guide/topics/ui/ui-events.html