How to track the down arrow button in virtual keyboard? - android

In the above figure i want to listen for down arrow( i have marked it in red colour, the key one which is used to hide the keyboard). I have tried to get it action through menu options, viewconfiguration class etc. But it is not useful. Is there any method, listeners or any callback event to track this button. Please let me know your suggestions and thoughts. Any help in this regard is highly appreciable
Thanks inAdvance

In your activity, override this method: http://developer.android.com/reference/android/app/Activity.html#onBackPressed()

If I am not you talking about this method
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
Toast.makeText(KeyActions.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}

Related

How to dispatchKeyEvent to currentView in TabHost

I need to change the KeyEvent dispatch in TabActivity. If the current tab content activity/view can handled the KeyEvent.KEYCODE_Back, let it handled it. If not, show dialog to tip like this:"Would you want exit?". How can do this?
I have tried by this code in my TabActivity:(can't implement my demand)
#Override
public boolean dispatchKeyEvent(Event event){
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
if(!mTabHost.getCurrentView.dispatchKeyEvent(event)){
showDialog(0);
return true;
}
}
return super.dispatchKeyEvent(event);
}
I think this link can make you clear.
you should override onKeyDown method, and directly call mTabHost.onKeyDown(...) method, check it return result, if false, it means you should show the exit message to user.
hope can help you.

Overriding back button press from Activity's subClass in Android

I'm making a game with OpenGL Es 1.1 and i want to implement back button or menu button functionality to my game.(I mean hardware buttons).I have some subClasses so what i have to do when i want to handle hardware button press from subClasses?
You need to implement an onKeyDown listener and check to see what key is pressed.
Sample:
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
handleMenuButton();
return true;
case KeyEvent.KEYCODE_BACK:
handleBackButton();
return true;
}
return super.onKeyDown(keycode, e);
}
Also note that for the back and menu buttons to fire reliably, you need to set setFocusableInTouchMode to true. See the devguide here (scroll down to "Touch Mode").

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.

KeyEvent handling in android?

I want to programattically handle the ENTER key in android.
Is there any way to achieve this?
Please Help!!
Thanks in advance
You need to carefully override the following function:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
{
//your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Use onKey() to capture the key. KEYCODE_NUMPAD_ENTER is used for the numeric ENTER key, and KEYCODE_DPAD_CENTER is the Directional Pad Center key.

Categories

Resources