IDK but i am continuously getting this issue To make request focus on EditText when Keyboard next button is pressed in adapter.
I have set the android:imeOptions="actionNext" in my adapter layout. When i am on first item of 'ListView' then its is working fine.
But when i press next from the keyboard. It goes to next position of EditText in ListView which is perfect but the EditText is not getting the requestFocus() and same problem with other next item.
I tired all the solutions i found out in stackoverflow . But i think i am unable to find out the best solution.
Could you please help me out.
Any help will deeply appriciated. Please check the screenshots below.
May be this will helps you :-
txtUserid.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
txtone.clearFocus();
txtsecond.requestFocus();
return true;
}
return false;
}
});
Related
so i have a login form. When i press the back button or the enter button, it seems to go the next edit text (or previous) even though i set these following options for my edit text.
What is weird, once i cycle through them all, everything works fine as it should i.e. back button / done button dismiss the edit text and key board.
Please note i am only showing 1 edit text field to not flood the post. The rest are pretty much the same
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.test.views.widgets.myEditText
android:id="#+id/userFirstName"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/generic.first.name"
android:inputType="textPersonName"
android:textColor="#android:color/white"
android:theme="#style/EditText.Base.Style"
app:font="regular" />
</android.support.design.widget.TextInputLayout>
So the above, i have set actionDone which was one of the suggestions to this problem. This does not work.
In addition, I am extending the AppCompatEditText to have my own implementation.
Here is some code
When done button is pressed
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_DONE){
clearFocus();
}
return false;
}
When the back button is pressed
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
clearFocus();
}
return false;
}
Also note that i followed this tutorial as a means to hide keyboard when focus changes
http://toastdroid.com/2015/06/04/on-screen-keyboard-tracking-part-2/
Add android:imeOptions="actionDone". It will close keyboard rather than going on next focusable view.
add this to the EditText attribute
android:maxLines="1"
Try adding android:imeOptions="actionDone" to your EditText
OR
youredittext.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int keyCode,
KeyEvent event) {
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
(keyCode == KeyEvent.KEYCODE_ENTER) )
{
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
return true;
}
return false;
}
});
I suppose that problem is in your clearFocus(). Documentation says:
Note: When a View clears focus the framework is trying to give focus
to the first focusable View from the top. Hence, if this View is the
first from the top that can take focus, then all callbacks related to
clearing focus will be invoked after which the framework will give
focus to this view.
try to replace clearFocus() with hideKeyboard() like this
void hideKeyboard() {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(yourView.getWindowToken(), 0);
}
I managed to fix this.
I added this to my root layout (which contained the edit text) and the problem no longer seems present
android:focusableInTouchMode="true"
I have a set of EditTexts in my app that the user needs to fill in. Pressing "Next" on the soft keyboard advances the focus to the next EditText and at the last one I capture the soft key Enter action to validate and submit the entered data. In case there is a validation error (e.g. an unfilled field) the relevant field (i.e. the unfilled field) requests focus whilst also showing a toast to display the error.
It works fine (focuses on the correct item) if I use a Send button which calls the same validation function, but if I hit the Enter button on the soft keyboard, the focus jumps to the EditText that caused the validation error and immediately moves to the one immediately after it.
This is the snippet that sets the listener on the last EditText.
lastEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if(validateFields()){ //Validation function
sendPressed(); //Submit function
}
return true;
}
return false;
}
});
Any help would be appreciated..
I have a list view where we have several items to be selected by user.
When ENTER key is pushed the call is going into onItemCLick()
How can I stop this behaviour ?
I want that the call should go into this method only when
user touches a list view item.
Try to hide the SoftKeyboard when the listview shows. I believe that you're testing your app on the emulator. When you test your app on the actual mobile you will not see the Enter key.
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
// Editable a=findViewById(R.id.username).getText();
// username2.setText(a);
Toast.makeText(yassou.this, username2.getText(),
Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
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.
For my android app I have multiple edittexts on the main screen. If i have the first edittext in focus the menu/back buttons operate fine, if i have any of the other edittexts in focus than neither of them work at all. I'm not doing anything special regarding the menu/back buttons relative to that edittext, i'm not sure what the cause is? Has anyone run into a similar issue and/or know of the cause/solution?
Thanks!
I was have the same problem and I found solution for I was have OnKeyListener that return true; when I change it to return false; the problem was fixed
my_editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// you can use the next line to ensure back button & menu button will return false
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_MENU) return false;
// any other key you don't want to call other listener for it
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER&& event.getAction() == KeyEvent.ACTION_UP) {
return true;
}
return false;
}
});
OnKeyListener.onKey(android.view.View, int, android.view.KeyEvent)
easy fix for me, I had removed the only keyboard app.
just install a keyboard app and the buttons worked again