I am trying to programmatically designate where the focus goes when a user presses the next button on the keyboard. I can get it to work for some, but I have one case where the view gets skipped and the view below it gets the focus.
I went into debugger and the focus does go through my view, but then jumps to the last one. Below is a small diagram of what I'm talking about.
(1) EditText
(2) AutoCompleteEditText
(3) EditText
(4) EditText
(1) -> (2) is ok
(2) -> (3) doesn't work, it does (2) -> (3) -> (4), where it briefly go to (3)
if I start at (3), then (3) -> (4) is ok.
This is how I am setting the focus
view.setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
view.clearFocus();
nextView.requestFocus();
return true;
}
return false;
}
});
view.setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if((event.getAction() == KeyEvent.ACTION_UP) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
view.clearFocus();
nextView.requestFocus();
return true;
}
return false;
}
});
Found the solution. If anyone else is having this issue, just change ACTION_DOWN to ACTION_UP so your code is only triggered when the press is complete. I'm guessing with ACTION_DOWN, there are multiple calls made. But I'm not sure.
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 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
I am creating login screen with 2 editTexts: etUsername and etPassword.
On the etUsername, user should input the username and press Enter to go to the edit text etPassword, then he inputs the password, press Enter to login. Here is my current code:
etUsername.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER)) {
etPassword.requestFocus();
return true;
} else
return false;
}
});
etPassword.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER)) {
loginToServer();
return true;
} else
return false;
}
});
But when I input the username, and then press Enter – the program tries to log in to the server.
In the debug mode, I saw that when I pressed Enter once (on the etUsername) then first: etUsername.onKey() is called and then etPassword.onKey() is also called !
How can I modify the code so that the ENTER event is only processed once for the current field?
Before you check which key is pressed, try adding a check for which key event occurs. I would guess that it first triggers on the key-down event, then you move focus to the second text field, and here it also triggers on the key-up event.
See here for an example on how to check both the event type and key: Use "ENTER" key on softkeyboard instead of clicking button
I'm not 100% sure of this, but it is worth a try :-)
Hello
I've got a searched EditText and search Button. When I type the searched text, I'd like to use ENTER key on softkeyboard instead of search Button to activate search function.
Thanks for help in advance.
You do it by setting a OnKeyListener on your EditText.
Here is a sample from my own code. I have an EditText named addCourseText, which will call the function addCourseFromTextBox when either the enter key or the d-pad is clicked.
addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
addCourseFromTextBox();
return true;
default:
break;
}
}
return false;
}
});
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND. For example:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
Source: https://developer.android.com/training/keyboard-input/style.html
may be you could add a attribute to your EditText like this:
android:imeOptions="actionSearch"
add an attribute to the EditText like
android:imeOptions="actionSearch"
this is the best way to do the function
and the imeOptions also have some other values like "go" 、"next"、"done" etc.
Most updated way to achieve this is:
Add this to your EditText in XML:
android:imeOptions="actionSearch"
Then in your Activity/Fragment:
EditText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Do what you want here
return#setOnEditorActionListener true
}
return#setOnEditorActionListener false
}
We can also use Kotlin lambda
editText.setOnKeyListener { _, keyCode, keyEvent ->
if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
Log.d("Android view component", "Enter button was pressed")
return#setOnKeyListener true
}
return#setOnKeyListener false
}
To avoid the focus advancing to the next editable field (if you have one) you might want to ignore the key-down events, but handle key-up events. I also prefer to filter first on the keyCode, assuming that it would be marginally more efficient. By the way, remember that returning true means that you have handled the event, so no other listener will. Anyway, here is my version.
ETFind.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// do nothing yet
} else if (event.getAction() == KeyEvent.ACTION_UP) {
findForward();
} // is there any other option here?...
// Regardless of what we did above,
// we do not want to propagate the Enter key up
// since it was our task to handle it.
return true;
} else {
// it is not an Enter key - let others handle the event
return false;
}
}
});
this is a sample of one of my app how i handle
//searching for the Edit Text in the view
final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
//do something
//true because you handle the event
return true;
}
return false;
}
});