My xml code for my Activity is
<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" > <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> TO add Key Event for this I have coded in main activity like this
public class MainActivity extends Activity { EditText e1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); e1=(EditText)findViewById(R.id.et1); /* e1.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { Log.i("keycode","Inside on key"); //char a=event.getDisplayLabel(); char a = (char)event.getUnicodeChar(); Log.i("keycode",Character.toString(a)); if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { Log.i("keycode",Integer.toString(keyCode)); Log.i("keycode",(e1.getText()).toString()); return true; } else { return false; } // TODO Auto-generated method stub } });*/ } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub Log.i("key","KeyPressed"+Integer.toString(keyCode)); Log.i("keycode",(e1.getText()).toString()); return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch(keyCode) { case KeyEvent.KEYCODE_A: { Log.i("key1","KeyPressed A"); Log.i("keycode1",(e1.getText()).toString()); return true; } } return super.onKeyUp(keyCode, event); } @Override public void onBackPressed() { // TODO Auto-generated method stub new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Closing Activity") .setMessage("Are you sure you want to close this activity?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton("No", null) .show(); } } when I press A from the soft Keypad...the method onKeyDown or onKeyUp is Not called...somebody say me the way to invoke these methods when i press alphabet in soft Keypad..