How to capture when user presses "Search Soft key" in android.
Can we do it in onKeyDown() and checking the keycode?
Try this in your Activity code:
#Override
public boolean onKeyDown( int keyCode, KeyEvent event )
{
if ( event.getKeyCode() == KeyEvent.KEYCODE_SEARCH )
{
// Catch the search
// do something
// return true to consume press or false to pass it on
}
return super.onKeyDown( keyCode, event );
}
I think that you are right. You can check for the key code that is given in the following list.
And prefer to check this onKeyUp.
http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_SEARCH
I've done something like this for capturing the search softkey when progress dialog is showing and it worked fine for me:
progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
return false;
}
});
Related
While doing digits programming, i am using following method :
Digits.authenticate(authCallback,R.style.CustomDigitsTheme1);
which directs me to digits authentication screen(without showing my xml design file).
Now, when i press back button, it shows me my xml with digits auth button as below.
which i do not want.I tried conventional ways of disabling back buttons but they did not work. Is there any way i can disable back button on authentication???
You can override when the keyboard disappears using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
This may helps you.
Try Using these
#Override
public void onBackPressed() {
// your code.
}
and for older then API 5 use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
i have been using dispatchKeyEvent to get the keycode of android keyboard but the problem is when i tap on Sym or Emoji button nothing happen, no keycode shows. this is the code i use to get the keycode:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(getBaseContext(), "key pressed : "+ String.valueOf(event.getKeyCode()), Toast.LENGTH_SHORT).show();
return super.dispatchKeyEvent(event);
}
Use KeyEvent.KEYCODE_PICTSYMBOLS for emoji.
/*
* Respond to soft keyboard events, look for the DONE press on the password field.
*/
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KeyEvent.KEYCODE_PICTSYMBOLS))
{
// Done pressed! Do something here.
}
// Returning false allows other listeners to react to the press.
return false;
}
I have an edit box which popup an android keyboard. What i want is on click of done button in the keyboard i want to display a message in the and if the user click rest of the buttons in-spite of done button the message should be clear. I have used this code but it doesn't work. Please help me to solve this out.
Code i have used
m_passwrdEditText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
m_passwrdErrorText.setText("Test Project");
}
else
{
m_passwrdErrorText.setText("");
}
m_passwrdEditText.setTypeface(face);
return false;
}
});
return true from onKey() method.
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.
I am new to android, I have a small doubt regarding how to handle the hardware Keyboard and if I click the search button in any part of my application it should be handled means I need to pass the intent of search activity?
How I can reach this goal.
Try this,
#Override
public boolean onSearchRequested() {
// your stuff here
return false;
}
It will also trigger onKeyDown with a keyCode of KeyEvent.KEYCODE_SEARCH before calling onSearchRequested as stated above
Add a listener for your EditText to listen on the Search button as following:
myEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_ENTER) {
//Do some stuff
}
//Leave the return value false to hide the SoftKeyboard if it is shown
return false;
}
});
For me, it happened before that the softkey search button is detected as Enter button not search. That's why I'm ORing them