I am new to Android development and having a problem with what SHOULD be a very simple task. I want to receive KeyEvents whenever a user is typing in an EditText field because I want to save their entered values to data structures in the background on each key stroke.
I have mimic'd the code in the Beginner's Dev guide at http://developer.android.com/resources/tutorials/views/hello-formstuff.html#EditText and set up an OnKeyListener. Here is a snippet of my code:
cell.amountEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.err.println("onKey for Amount, key="+event.getDisplayLabel());
if (event.getAction() == KeyEvent.ACTION_DOWN) {
return onKeyDownInAmount(finalPosition, (EditText)v, keyCode, event);
} else {
return false;
}
}
});
Behavior on the emulator is spotty at best, some times it will deliver the KeyEvents for the virtual keyboard, sometimes it won't. When I install the app on my device (HTC Hero which has a virtual keyboard only) then NONE of the events fire. I never receive a single KeyEvent.
What am I doing wrong?
Any help is appreciated.
The onKeyListener only receives events from a hardware keyboard. Use TextWatcher instead.
Related
I want to build a instant searchview, which will show the search result immediately in the same layout as soon as a user input anything. So I am googling and decide to use onQueryTextChange to implement that. However I cannot figure out how to "fire" a intent like the normal result which pressing ENTER will lead to, to tell my Activity that there is an event that a user have input something in searchview and you(Activity) should handle it.
Any idea to help?
If I understood you correcty you want to recognize KEY_EVENT (ENTER). I've ended with custom EditText styled like original SearchView, then used OnEditorActionListener:
queryEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
(event != null && event.getKeyCode()==KeyEvent.KEYCODE_ENTER)) {
performSearch();
return true;
}
return false;
}
});
SearchView don't have setOnEditorActionListener method, thats why I'm using simple EditText
note EditorInfo.IME_ACTION_SEARCH option inside "if", you may request "special" keyboard layout with magnify icon instead of Enter key (but remember that not all keyboards are supporting this option and custom keyboard app may show "usual" layout of keyboard)
android:inputType="text|textNoSuggestions"
android:imeOptions="actionSearch"
I'm trying to make an app which is controlled by a gamepad. I've gotten it to work alright, but Android has some default controls that it uses for navigation when a gamepad is plugged in, such as the B button takes you back a menu. I want to be able to use the buttons that Android has defaults for. Is there a way to disable the default Android controls? I can't find any thing about the default Android gamepad controls, let alone how to disable them.
I figured it out. For anyone who needs this in the future, here's how to do it. When you add in the onKeyDown override command, this is what it looks like.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return super.onKeyDown(keyCode, event);
}
As I understand it, that return line gives the Android system access to the button presses. However if you make it always return true, the Android system never sees the input. For example:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BUTTON_A)
{
buttonAPressed = true;
}
return true;
}
I don't know if this is the best way to do it, but that's my work around to it. Hope this helps anyone that needs it!
I need to get some information from the on screen keyboard such as pressure, KeyDown and KeyUp in Android but don't know how to do that.
The android official site says that:
Note: When handling keyboard events with the KeyEvent class and
related APIs, you should expect that such keyboard events come only
from a hardware keyboard. You should never rely on receiving key
events for any key on a soft input method (an on-screen keyboard).
I also tried the following method without success. It actually works with the hardware keys like back button.
myEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
return false;
}
});
I was thinking of finding a way of extending the on screen keyboard, but still no success!
Does anyone have ever tried doing this? I'd appreciate your help in advance.
UPDATE:
After trying many solutions I came up with the same solution of using my own keyboard, suggested by krossovochkin, ultimately. It seems that the solution is not too bad if one wants to modify the Android's keyboard as a new one. This way, it appears in the "Settings --> Input Methods" so that the user can switch to the new keyboard, which is good since it is accessible from all other apps. Since it is not yet clear that whether it is possible to get the pressure from the standard virtual keyboard, therefore I thought the question could be left open.
You can try to call getPressure() method from MotionEvent
Link: http://developer.android.com/reference/android/view/MotionEvent.html#getPressure%28int%29
Code snippet:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float pressure = event.getPressure();
}
});
UPDATE:
You can create your own keyboard and getPressure from it.
Maybe user will not like using your keyboard instead of his default keyboard.
But I think this is the best solution for your situation.
More information about:
KeyboardView: http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html
Example of creating your own keyboard:
https://github.com/rciovati/Android-KeyboardView-Example
I have 6 Edittexts grouped in 6 differents layouts, all in the same view.
My problem is that my app is forced to landscape mode, and by pressing the 'next' button i want to automatically start to edit another editText rather than the one android set me by default. Example: i'm writing on EditText1, press next, and focus is taken by the EditText that is UNDER the 1st one. I want to edit the one that is RIGHT to it.
Sorry for my bad english :(
The device is a galaxy tab, with Google API8/Android 2.2
Thanks
Haven't tested it, but this should work
some_edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
some_button.performClick();
return true;
}
return false;
}
});
I might be wrong but I don't think there is any implicit way of doing it. You would need to write your own EditorAction.
You need to monitor everything that the user enters and whenever he presses 'NEXT', you would have to manually shift the focus to the next EditText, using EditorInfo.IME_ACTION_NEXT.
Doing a simple override in my base activity of onKeyDown, I'm able to capture all key presses except those of the enter and dpad center buttons (as determined via breakpoint). I've no clue as to why - can anyone shed some light on the situation?
EDIT: Quick update - it does capture Dpad center and enter key LONG presses, but still not normal presses.
I know this question is already pretty old but in case some desperate coder got lost I post my answer.
I had a similar problem with my USB keyboard. When anything else except a EditText box was focussed the ENTER key was never caught by onKeyUp or onKeyDown.
if you use dispatchKeyEvent() you get the KeyEvent before it reaches the window and in my case I definitely get the ENTER key.
But cautious, the event is called twice, once for key down and once for key up.
here a sample code:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
System.out.println(event.getAction() + " " + event.getKeyCode() + " - " + (char) event.getUnicodeChar());
return true;
}
Did you read the documentation?
Key presses in software keyboards will generally NOT trigger this listener, although some may elect to do so in some situations. Do not rely on this to catch software key presses.
Also, your way of capturing keys is very vague. You are not even checking the keyCode sent to you by using:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) { return false; }
You can handle onKey from a View:
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}
Remember to implement OnKeyListener and to set your listener
viewname.setOnKeyListener(this);
it appears that DPAD keys are acting for the focused items as have been told here:
https://groups.google.com/forum/#!topic/android-developers/HsILBlpsK7I
Although I haven't tried it myself, maybe you can set focus to your view object and attach a key listener function on it.
Update: My colleague had the same problem and this suggestion worked for her. :) She was able to catch the key code from DPAD when the list view is focused.