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.
Related
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 ;
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. :)
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.
For my android app I have multiple edittexts on the main screen. If i have the first edittext in focus the menu/back buttons operate fine, if i have any of the other edittexts in focus than neither of them work at all. I'm not doing anything special regarding the menu/back buttons relative to that edittext, i'm not sure what the cause is? Has anyone run into a similar issue and/or know of the cause/solution?
Thanks!
I was have the same problem and I found solution for I was have OnKeyListener that return true; when I change it to return false; the problem was fixed
my_editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// you can use the next line to ensure back button & menu button will return false
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_MENU) return false;
// any other key you don't want to call other listener for it
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER&& event.getAction() == KeyEvent.ACTION_UP) {
return true;
}
return false;
}
});
OnKeyListener.onKey(android.view.View, int, android.view.KeyEvent)
easy fix for me, I had removed the only keyboard app.
just install a keyboard app and the buttons worked again
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
public boolean onKey() called twice?
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
solveExpression();
return true;
}
return false;
}
});
I'm trying to solve the expression contained within the Display(EditText), by pressing the enter button on the keyboard, yet it always interprets it as though I pressed the button twice. Does anyone know why this happens?
Try...
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// Check for ACTION_DOWN only...
if (KeyEvent.ACTION_DOWN == event.getAction()) {
solveExpression();
return true;
}
}
}
});
The 'action' can be ACTION_DOWN, ACTION_UP or ACTION_MULTIPLE (the last being for when a key is pressed and held). onKey() will be called for any/all of those actions.
As the other answer mentions, it's triggering twice because it's once for down and once for up.
if (event.getAction()!=KeyEvent.ACTION_DOWN) // we catch only key down events
return true;
Thus you stop listening other keyevents as onClick.
If you want nobody else further in chain to get the event for another piece of work, you should set
return false;
not an android guy either but the fact that it registers twice makes me think that OnKey encompasses a onKeyDown and onKeyUp. Would listening to onKeyUp work for you as well?