how to handle numeric keys from the phone keypad - android

In following keypad:
we can select value by pressing buttons (e.g View button1.setOnClickListener(..)), all i want to know is how can i handle keys(keys 0 to 9, enter_key, clear_key) from the phone keypad (e.g. when running on device other then tablet).
Please give a quick refference. any online tutorial will be highly helpfull.
regards,

you can go for onKeyDown which takes keycode and KeyEvent as arguments and KeyEvent class contains all the numeric codes:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_0){
//do some stuffs
}else if(keyCode==KeyEvent.KEYCODE_1) {
// do some stuffs
}
return super.onKeyDown(keyCode, event);
}
Like this you can handle numeric key press/.Hope it wil help you. :)

Related

How to listener the soft keyboard key event

how to know when I press the delete key from the soft keyboard in android, an event the content of the edittext is empty. I know when the content is not empty, can use TextChangeListener. Only the empty content how to listen the pressed key.
KeyEvent provides keyboard events
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do Code here
}
return super.onKeyDown(keyCode, event);
}
setListener is not worked in same android system. Finally I got a smart idea, which can use a space replace by ImageSpan,and set picture size is 0*0. So there always will not be empty.

Getting the pressed character without using edidtext

I want to get a pressed character in the code without using edittext.
Simply a black screen appear if someone presses "a" or "b" etc.. I want to get this value in my code.
I have done it using editext and textwatcher but the requirement is we don't need to use them now.
Can we make character(alphabet) moving from top to bottom randomly
I have followed some links which have used marquee but it does not work.
In your Activity override the method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_A) {
//Here your black screen if you press A
return true;
}
return super.onKeyDown(keyCode, event);
}
Check this KeyEvent ;

How can I create a long touch event on the physical menu button?

Right now when you hold down the menu button on my android device it pulls up a soft keyboard.Is there a way to override this? I would rather choose what happens on a long touch of this button.
When using onKeyLongPress it only detects when the "Back" button is held down. How can I make this work for the menu button?
For this, you can use the onKeyLongPress()-method, offered by the KeyEvent.Callback-class (can be used in Activity's too, since they are a subclass of the KeyEvent.Callback-class).
There is also a little trick to make this work: You'll have to tell Android to track a long-press click on the "Menu"-button as the onKeyLongPress()-method will not be triggered otherwise. This is done in the normal onKeyDown()-method.
So your code might look like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_MENU){
// Do what you want...
Toast.makeText(this, "I'm down!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyLongPress(keyCode,event);
}
A great article with further informations can be found on the Android Developer Blog.

Event when user select back?

How can I catch the event when the user, after he entered the text on an EditText, select Back and the keyboard disappear?
Override onBackPressed at your Activity class:
#Override
public void onBackPressed() {
//If keyboard is on, do what you want
super.onBackPressed();
}
If you want to catch the back key when user has finished entering text in EditText and he presses back key then you should use:
EditText edit = (EditText)findViewById(R.id.yourId);
edit.setOnKeyListener(new EditText.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
System.out.println("******back key caught in edit.setOnKeyListener");
}
return false;
}
});
I had the same problem. Vineet Shukla's answer was not working, until I made sure that the delegate was a EditText.OnKeyListener. Prior to that it was a View.OnKeyListener and I was not seeing KeyEvent.KEYCODE_BACK == keyCode, for was I even seeing onKey ever called. I hope this is helpful to someone having a similar problem, although this post is a year old. Cheers.

OnKeyListner is not working

Hi Any reply is precious for me and appriciated as well
in one edit text i am setting onKeyListener so that when I enter 5 numerics in edittext it will accept n do next process but in samsung galaxy tablet it is not working i am using these lines of code
zipcode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("setOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListener");
if (event.getAction() == KeyEvent.ACTION_UP && zipcode.getText().length() == 5) {
System.out.println("OnKeyListener11111");
started = true;
searchByZipcode(zipcode.getText().toString());
}
return false;
}
});
searchByZipcode(zipcode.getText().toString());
line takes the text we are writing in to webservice but in galaxy flow doesnt get into onKeylistner can any1 pls help me out thanks
That's odd. Only in Samsung Galaxys? Is this the full Listener? Maybe you have some other method catching KeyEvents that return true?
Try replacing the code for onKey() by onKeyUp(), which is a TextView method:
http://developer.android.com/reference/android/widget/TextView.html#onKeyUp%28int,%20android.view.KeyEvent%29
BTW, why do you add return false;? Is there any other Listener that requires to handle the KeyEvent? If not, I would suggest to change it for return true;
If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

Categories

Resources