Android - SPLessons

Android Event Handling

Chapter 11

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android Event Handling

Android Event Handling

Android Event handling gives you an overview of different types of Event handlers and Event Listeners. An Event is a response to an user’s interaction with input controls, such as press of a button or touch of the screen. Android framework places each occurring Event in a queue based on FIFO (first-in first-out) logic.

When an Event triggers, the Event Listener that is involved with the View object should be registered. Then, the registered Event Listener should implement a corresponding callback method (Event Handler) in order to handle the Event.

There are different ways to register an Event Listener.

Some of the most common Event Handlers with the respective Event Listeners are as follows:

onClick() – onClickListener(): The system calls this method when a user clicks a View component.

Ex:

Button click=(Button)findViewById(R.id.click);
        click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
            // TODO Auto-generated method stub

            }
        });

onLongClick() – onLongClickListener(): This method is called when a user clicks or holds a View component for more than one second.

Ex:

 Button click=(Button)findViewById(R.id.click);
        click.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                // TODO Auto-generated method stub
                return false;
            }
        });

onTouch() – onTouchListener(): This method is called when a user performs actions like pressing the key, releasing the key or any movement gesture on the screen.

Ex:

ImageView touch=( ImageView)findViewById(R.id.touch);
        touch.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                int action = arg1.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:

                        break;
                    case MotionEvent.ACTION_MOVE:

                        break;
                    case MotionEvent.ACTION_UP:

                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                    default:
                        break;
                }
                return true;
            }

        });

OnKey() – onKeyListener(): This method is called when a hardware key is pressed or released on the device.

Ex:

EditText ed=(EditText)findViewById(R.id.editText1);
        ed.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
            // TODO Auto-generated method stub
                return false;
            }
        });

onFocusChange() – onFocusChangeListener(): This method is called when a user navigates towards or away from an item.

Ex:

EditText ed=(EditText)findViewById(R.id.editText1);
        ed.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
            }
        }
        });