How does EditText still have focus after the soft keyboard is hidden (user presses Done/Enter/Search button)?
Could you please provide me some explanation about this?
That's the default Android behaviour. You can call view.clearFocus() on Enter key press to override this.
Your app and Android soft keyboard (stock one) are different app processes. And while Android takes care of "notifying" the soft keyboard app on your app's EditText focus requests, neither Android nor the soft keyboard app should be responsible for your app's EditText focus changes. Your app has enough callbacks from the soft keyboard at its disposal to clear focus.
- Add Following Line of Code in your Specific Activity.
EditText edt_user = findViewById(R.id.edt_user);
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_user,0);
edt_user.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
edt_user.clearFocus();
}
return false;
}
});
Related
I am making small application. I use EditText to summarize all data. It shows a few times, every time has own line. User is able to write short note next to time.
I have issue with soft keyboard. I would like it to have "ok" instead of enter. I mean, it shoul not do extra line. On enter, keyboard should close.
For now, I have something like this:
EtNotes.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
return false;
}
});
It closes keyboard, but it does extra line and that is the problem.
I have already tried to put:
android:imeOptions="actionDone"
in my layout xml file. Unfortunately it also didn't worked.
To sum up, I would like my keyboard don't do extra line and closes after enter button.
You have to return true in the branch where you handled the key input
I am working with Amazon S3 api.
Problem: Working with TextWatcher. As soon as user enters more than 3 characters in the edittext, api is called and it shows matched results. The problem starts as soon the user hit the done/enter button on soft keyboard, the api is called again and it searches again. Can i stop this second call somehow? Thanks!
you can change it from xml:
<EditText
...
android:imeOptions="actionNone"/>
or from code:
editText.setImeOptions(EditorInfo.IME_ACTION_NONE);
It will remove the done button whenever user trigger the softkeybord
Maybe you can try to override the onKeyListener method in your control, to let it detect if the key pressed is 'enter' and add the code you want your 'enter' key to do ?
edittext.setOnKeyListener(new View.OnKeyListener() {
just handle done click and hide the soft keyboard
editText = (EditText) findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// hide your keyboard here
try {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager != null && activity.getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
return false;
}
});
Add
android:maxLines="1"
to you xml and enter button should be disabled.
You can Do like editText.setImeOptions(EditorInfo.IME_ACTION_NONE);
but it won't hide the Enter/Next
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..
When I'm using "EditText" I have the virtual keyboard.
Pressing first time "Back" button hides the keyboard. Second press invokes "onBackPressed" callback in my activity. OK, but...
I have no idea how to hook the very first press. I need to process input data as soon as the virtual keyboard dismissed.
Any ideas are welcome.
Thanks.
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);
}
Taken from my other answer # : Android: Error popup on EditText doesn't move down when keyboard goes away
Custom Back Button:-
final RelativeLayout rrBack = (RelativeLayout) mCustomView.findViewById(R.id.rr_back);
rrBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MyApplication.getInstance().getRequestQueue().cancelAll(FEED_DETAIL_TAG_REQUEST);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(rrBack.getWindowToken(), 0);
}
});
After a user inputs into an editbox with the keypad, I want the keypad to hide so I can see the results near the bottom of the screen. I've tried this in my onCreate method but it doesn't work. What am I missing?
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditTextbox.getWindowToken(), 0);
You can force hide the keyboard like so:
Window window = getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I would get the window in your onCreate method and in the onFocusChanged() method or OnKeyListener() hide the keyboard.
Have you set up a Key Listener?
You don't really state how you know the user is done entering the text so I'll assume they are pressing the enter button on the soft keyboard. Here is how I am handling that type of scenario. I am using this both in a Dialog and an Activity with success. Hope it helps.
this.setOnKeyListener(new OnKeyListener()
{
/**
* This listens for the user to press the enter button on
* the keyboard and then hides the virtual keyboard
*/
#Override
public boolean onKey(DialogInterface dialog, 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) )
{
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(sessionTag.getWindowToken(), 0);
return true;
}
return false;
}
});
It depends on what currently has focus...if its another editText that takes focus then this might be bringing up the keypad...try to explicitly give focus to a different element...
You can extend EditText class and use your code in onFocusChanged() method when focused argument is false.
Follow this answer. call the method after you setContentView in your activity.
https://stackoverflow.com/a/11656129/1066839