Android long key press - android

I need to generate/trigger a long key press event of a button in Android.
Any help regrading this?

From Android 2.0, Activity contains the method
public boolean onKeyLongPress(int keyCode, KeyEvent event)
For exemple, a long key press on the back button would be :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// do your stuff here
return true;
}
return super.onKeyLongPress(keyCode, event);
}
Take a look at this article.

You can set Long key press on button like:
btnNext.setLongClickable(true);
btnNext.setOnLongClickListener(l)

Related

How to obtain the currently selected View onKeyDown method?

I'm trying find if a specific View is selected following a key press. For this purpose I was trying to use the onKeyDown method on my main activity.
I already tried to set a setOnKeyListener on the fragment but it wasn't registering any input.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
// Check if it's selecting a specific View
}
return super.onKeyDown(keyCode, event);
}
Any idea how I could do this?
one from many approach that may use, fun n silly maybe, but it was work for me
on Global declaration
Integer iLastView=0;
on View that selected before
iLastView=iView;
and last in the inKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
if(iLastView==iView1){
//something here
}else if(check){.....}
}
return super.onKeyDown(keyCode, event);
}

How to detect if the keycode_numpad_dot is clicked in Android?

I'm a newbie on this language. I am struggling on this problem detecting if the keycode_numpad_dot is clicked.
For numpad dot use:
KeyEvent.VK_DECIMAL
Sample code:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.VK_DECIMAL)
return true;
else return super.onKeyDown(keyCode, event);
}
Android Key Code:
key 83 NUMPAD_DOT

How to listen the keypress in the soft keyboard?

I need a listener to identify the keypress in the soft keyboard/on screen keyboard.
I tried with addtextchangelistener textwatcher but this one give the good result but it shows the change also when some text is pasted into it.
I need to identify only the key press by the user.
Is there any possible way to detect the key press.
see this keyevent and use following code to identify which key is pressed by Users.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// Do Code here
}
else if(keyCode == KeyEvent.KEYCODE_0)
{
}
else if(keyCode == KeyEvent.KEYCODE_1)
{
}
return super.onKeyDown(keyCode, event); }
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).
see: Handling Keyboard Actions
See this if can help you.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 1) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do Code here
}
return super.onKeyDown(keyCode, event);
}

How to keep DropDownList of AutoCompleteTextView opened after pressing the Back key?

i am using AutoCompleteTextView in my Activity and i need it's DropDownList to be shown all the time (it's the only View in Window), even after Back key press. I need to dismiss soft keyboard instead.
I tried to override Activity's onBackPressed method, but it's not used at all, so BackPressed event is being handled somewhere "higher". So i tried to find out where, but AutoCompleteTextView has no onBackPressed method defined.
Any advices?
You can create your custom AutoCompleteTextView and Override the method onKeyPreIme(int keyCode, KeyEvent event)
I also realized that this method is called 2 times, I'm running my code only in the second time. Here is the example:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
//add your code here
return true;
}
return super.onKeyPreIme(keyCode, event);
}
You may try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Your back key press logic
}
return true;
}
Remember to return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.

Android listen for backbutton longclick

I am overriding the backbutton with a onBackPressed() function
how do I also detect long clicks on the backbutton? Is there an equivalent of #Override onBackLongPressed() ?
This might help you (Check the first comment) - Android long key press
From Android 2.0, Activity contains the method
public boolean onKeyLongPress(int keyCode, KeyEvent event)
For exemple, a long key press on the back button would be :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// do your stuff here
return true;
}
return super.onKeyLongPress(keyCode, event);
}
Check "Story 2" here. There's not a shortcut for it like there is onBackPressed().
I think you'll have to use onKeyLongPress and handle the KEYCODE_BACK event yourself.

Categories

Resources