Android onKeyDown() not execute on pressing delete button - android

I use the following method to delete values from EditText when user presses the delete button of an android device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
onDeleteKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void onDeleteKey() {
if (edt_passcode1.isFocused()) {
} else if (edt_passcode2.isFocused()) {
edt_passcode1.requestFocus();
edt_passcode1.setText("");
} else if (edt_passcode3.isFocused()) {
edt_passcode2.requestFocus();
edt_passcode2.setText("");
} else if (edt_passcode4.isFocused()) {
edt_passcode3.requestFocus();
edt_passcode3.setText("");
}
}
I want to delete the previous EditText value when user presses the delete button on their device, but it's not called when the delete button is pressed. But onKeyDown() method doesn't called.
Please help me about it.
Thanks.

From the docs of activity:
Called when a key was pressed down and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
Im guessing thats your problem?
Link:
http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29
EDIT:
One possible solution would be to attach a custom onKeyListener to your TextView. It is not necessary to subclass TexView like a commenter suggested, although thats an option as well.
You can get a template for the keyListener from this SO-Question:
Get the key pressed in an EditText
NEXT EDIT:
I just realized the first solution will only work for hardware keyboards.
If you want Virtual Keyboard Support, my best guess wild be an TextWatcher.
See a Sample implementation here:
Validating edittext in Android
NEXT AND HOPEFULLY FINAL EDIT:
Some googling turned up Activity's dispatchKeyEvent() - method. just implement it like you did with onKeyDown, you will be passed a KeyEvent that will tell you all about the pressed key.
Here's a link to the docs:
http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29

Related

Keypress on xamarin form not entry

I have a Xamarin Android form (content page) and I want when the user presses the a certain keyboard key to take a certain action, like calling a method. I can not find anywhere any solution to that.
If there is anyone knowing how to do this I would really appreciate his/her help.
Thank you
Try overriding DispatchKeyEvent in your MainActivity
public override bool DispatchKeyEvent(KeyEvent e)
{
// if the Keycode is A .. there's lots of these
//you'll have to checkout the Keycode class for more options
//this example will fire the following code when 'A' is pressed
if (e.KeyCode == Keycode.A)
{
//invoke your method here
}
//return base if needed, otherwise keystrokes won't get automatically passed into textboxes etc and stuff will act wacky
return base.DispatchKeyEvent(e);
}
https://learn.microsoft.com/en-us/dotnet/api/android.app.activity.dispatchkeyevent?view=xamarin-android-sdk-9
if this doesn't work you can also override OnKeyDown and OnKeyUp and OnKey AFAIK
Difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?

LibGdx: Move camera with Keyboard visible, capture Android BACK button

I have Actors that I need to move when the keyboard becomes visible (When I press a TextField), or they are stuck behind it. I do this by moving the camera up:
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/3, 0);
stage.getViewport().getCamera().update();
This works fine. It also works fine to move it back when I touch something outside the TextField and call stage.unfocusAll();
My problem is, when I'm in a TextField and press Androids Back button, it hides the keyboard, but does not call the code I have inside the Inputprocessor (THIS CODE CAPTURES BACK-BUTTON ALWAYS EXCEPT WHEN INSIDE A TEXTFIELD AND KEYBOARD IS VISIBLE):
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Input.Keys.ESCAPE) || (keycode == Input.Keys.BACK) )
{
moveBack();
}
return false;
}
};
I looked around and read that it is not possible to catch the back-button when inside a TextField. Which leads me to my questions:
This must be a common scenario (moving UI to work with keyboard), how do other people do it?
If other people do like me (move camera), how do you handle the Android back-button?
EDIT: This answer captures the back-key while inside a TextField, however it has to be done in the Android Launcher, so I can't reach the elements I need to reach. I also overwrites all other calls to the BACK-button from inside the LibGdx project.
Ok so with the help of This I got a solution working, but it's not very pretty.
Basically I rewrite my GDX Launcher class, and use the following code as the layout:
RelativeLayout layout = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
game.setMoveBack(true); }
}
return super.dispatchKeyEventPreIme(event);
}
};
Then, in the render-method of my MainScreen (Screen that all other Screens inherit from), inside the render-method I call:
if(game.isMoveBack()){
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/2, 0);
stage.getViewport().getCamera().update();
game.setMoveBack(false);
}
If anyone has an easier way to do this please post and Ill accept that as an answer, but for now, this works if anyone finds themselves where I am :)

Cancel Touch Eventbutt

I am writing an very simple application with following scenario:
1) Screen A have 3 button to move on other screen's.
2) Now if I hold one button(say Button 1) and perform rapid click on other button then it launch multiple instance of other screen. Which I think should not be happened. How can prevent this.
3) and it's more weird. After move on other screen if I don't release Button 1 which was on Screen A then it still allow to perform click for rest of two button of screen A even I can see second screen.
Here it's clear launch second screen but still first screen button event working.
Any idea how can avoid such scenario.
How you are going to disable other buttons while having 1 enabled, that's an algorhytmic problem. You can try creating a boolean or control variable in your activity (and then pass the final reference of the activity to wherever you need it), or in a static context. But to answer the title of the question - you can "Cancel Touch Event" either by adding an OnTouchListener, or if you're extending class Button, you can override onTouchEvent(MotionEvent ev) method.
Using OnTouchListener will disable any previously defined touch-event behavior. You can call the actual click event from the inside by calling performClick method from your button.
//in order to use button inside OnTouchEvent, its reference must be final
//if it's not, create a new final reference to your button, like this:
final finalButton = button;
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
if(canBeClicked) {
finalButton.performClick();
return true;
}
else return false;
}
}
Overriding onTouchEvent in a class extending Button should look something like this.
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
//here we don't really need to call performClick(), although API recommends it
//we just send the touch event to the super-class and let it handle the situation.
if(activity.canBeClicked) return super.onTouchEvent(ev);
else return false;
}
One solution that I found is to disable click listener in onPause() and enable it in onResume() . Can we have better support for this?

Android avd arrow keys not working

I am learning app development from a book, and have run into a problem with the avd. I have my code set up to change some text when the right arrow key is pressed:
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
textUpdate();
return true;
}
return false;
}
When I run the virtual device and press the arrow keys nothing happens. When I press the arrow keys on my physical keyboard, still nothing. I have done a lot of research and can't find the solution. I have tried editing the avd settings and editing the device(Nexus One) itself to accept keyboard and dpad input. What should I try now?
Have you overriden the method?
There should be an #Override above that function.
Override calls that function whenever its triggered so you can modify it to your specific implementation.
Hope this helps
the "arrow key" means the back key on android device?
if it is then you should override the onBackPressed
#Override
public void onBackPressed() {
// do something
}
Alright, what happened was my onKeyDown() function was inside a Fragment. I don't know why, but when I moved this code into the mainActivity it worked fine. If anyone has an answer as to why this worked and how I could get it to work in the fragment please comment.
Thanks

Edittext help - onKeyListener doesn't work

I have a simple edittext that I've added to my layout. I added a simple OnKeyListener as follows in the onCreate method:
final EditText simpleEditText = (EditText) findViewById(R.id.editText1);
simpleEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
System.out.println("HELLO");
return false;
}
});
When the application starts, everything works correctly. For every key pressed, HELLO is printed twice - once for DOWN action and once for UP action.
Now when I click on the edittext with my mouse, the edittext no longer runs the code in my onKey method. What am I doing wrong? I'd still like to receive key events after the user has clicked in the edittext. I don't care about the mouse click, I'm only using that to reposition the cursor. After the cursor is repositioned, the key events are no longer registered.
Please see the attached image to see more information.
Thanks
Zamil
http://i.stack.imgur.com/j2DP7.jpg
As per android API, onKey works only for the physical key that was pressed. You may need to consider OnClickListener for mouse clicks. Here is API documentation.
Click events are not key events. You won't (and shouldn't) receive onKey events for anything except key events.

Categories

Resources