Wednesday, December 4, 2013

Key event handling in android: Android tutorial

Program description:

This program depicts how to handle hard keyboard key events in an android program.

This contains one activity, which has editText. This program will show how the keyevents will flow to edittext and how to handle those key events. 


This program also shows how to handle key events which directly comes to activity.


Note: On running this program if key 0 is pressed by user, then it will not get printed in the edit text because we are returning true, which says don't handle 0.


Note: This will also cover how to handle back key, which is very important.

It uses onBackPressed to handle it.

Important functions covered in this program:
setOnKeyListener,
onKeyDown,
onKeyUp,
onBackPressed


First Activity
package com.techpalle.keys;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

  EditText et;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.editText1);        
        et.setOnKeyListener(new OnKeyListener() {
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
    
    int act = event.getAction();
    int rep = event.getRepeatCount();
    switch(act){
    case KeyEvent.ACTION_DOWN:
     Toast.makeText(getApplicationContext(), 
       "key down.."+rep, 0).show();
     if(keyCode == KeyEvent.KEYCODE_0){
      return true;
     }else if(keyCode == KeyEvent.KEYCODE_BACK){
      Toast.makeText(getApplicationContext(), 
        "et-back", 0).show();
     }
     break;
    case KeyEvent.ACTION_UP:
     Toast.makeText(getApplicationContext(), 
       "key up.."+rep, 0).show();
     break;
    case KeyEvent.ACTION_MULTIPLE:
     Toast.makeText(getApplicationContext(), 
       "key multiple.."+rep, 0).show();
     break;
    }
    return false;
   }
  });
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
  Toast.makeText(getApplicationContext(), 
    "activity key down..", 0).show();
     return super.onKeyDown(keyCode, event);
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
  Toast.makeText(getApplicationContext(), 
    "activity key up..", 0).show();
     return super.onKeyUp(keyCode, event);
    }
    @Override
    public void onBackPressed() {
     // TODO Auto-generated method stub
     Toast.makeText(this, "back pressed", 0).show();
     super.onBackPressed();
    }
}




xml layout file for First Activity 
File name: activity_main.xml

 <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="@string/hello_world" />  
   <EditText  
     android:id="@+id/editText1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_below="@+id/textView1"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="40dp"  
     android:ems="10" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_below="@+id/editText1"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="42dp"  
     android:text="Button" />  
 </RelativeLayout>  

Download complete code : Click to download

Tags : key event handling in android, keycode, keyevent, action_down, action_up, setOnKeyListener, onkeydown, onkeyup, onbackpressed

No comments:

Post a Comment