12

Is an edittext cursor supposed to continue blinking after the soft keyboard is closed or is this a result of testing on an emulator and wouldn't happen on an actual device? -- as pointed out by the second post in this discussion

Update:

I know that the edittexts still have the cursor blinking because they're still in focus -- logged a message whenever edittext lost focus, but message was never logged when soft keyboard closed.

Update:

I've tried doing:

@Override
public void onBackPressed() {
    super.onBackPressed();
    getCurrentFocus().clearFocus();
}

So that every time the keyboard is closed, the EditText currently in focus loses that focus and onFocusChanged() is called. The problem is that onBackPressed() isn't called when the back button is pressed when the keyboard is up. I know this because I put a toast in onBackPressed(), and no toast shows when the back button is pressed whilst the keyboard is up.

the beest
  • 463
  • 6
  • 26
  • That's because your last edit text **never** lost focus when you close the soft keyboard. You can add `setOnKeyListener` to your edit text and change focus to another layout component when you press _done button_ (Enter) or _Back button_ . – JJ86 Sep 07 '16 at 11:53
  • @JJ86 i can't use `setOnKeyListener` because there are optional EditTexts, so it's impossible to know whether the user wants to fill in the next optional EditText or has finished all together when they press the _done button_. i've tried overriding `onBackPressed`, but i run into [this problem](http://stackoverflow.com/questions/39318948/getcurrentfocus-clearfocus-isnt-removing-focus-even-when-the-root-view-has) and haven't figured out a solution yet – the beest Sep 07 '16 at 14:48
  • I see, but what about combining it with `setOnFocusChangeListener` and detect which one had focus or not? – JJ86 Sep 07 '16 at 16:08
  • @JJ86 it doesn't matter which one has focus unless it's the last one, in which case there's only one action to take -- to submit the form. as for the others, there's is no definitive way to determine if a user wants to go to the next edittext or submit the form when they click Enter based on whether it has focus or not. – the beest Sep 10 '16 at 18:54

7 Answers7

1

First create a custom Edit text. Below is the example which has a call back when keyboard back is pressed to dismiss the keyboard

public class EdittextListner extends EditText {

private KeyImeChange keyImeChangeListener;

public EdittextListner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setKeyImeChangeListener(KeyImeChange listener) {
    keyImeChangeListener = listener;
}

public interface KeyImeChange {
    public boolean onKeyIme(int keyCode, KeyEvent event);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyImeChangeListener != null) {
        return keyImeChangeListener.onKeyIme(keyCode, event);
    }
    return false;
}

}

Secondly change your EditText to EdittextListner in you layout file.

Finally do the following

 mLastNameEditText.setKeyImeChangeListener(new EdittextListner.KeyImeChange() {
        @Override
        public boolean onKeyIme(int keyCode, KeyEvent event) {
            mLastNameEditText.clearFocus();
            return true;
        }
    });

This worked for me. Hope this helps

Avinash4551
  • 220
  • 1
  • 9
  • how does this code distinguish between the back button being pressed and other keys? – the beest Sep 10 '16 at 19:24
  • this doesn't work for me. does this detect when the soft keyboard's back button is pressed, a hardware keyboard's back button, or the phone's hardware back button? – the beest Sep 10 '16 at 19:52
  • In the overridden method onKeyPreIme() you can check for key back pressed like below if (event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_BACK) This code detects the hardware key back press... – Avinash4551 Sep 12 '16 at 18:01
1

Edittext is a View which accept input from user, so it is not related with keyborad open or close, when user will click on edittext, that edittext will get focus and cursor will start to blink for taking input,

So you can do one thing as when you are closing keyboard at the same time you can also set visibility of cursor for that edittext so it will stop to blink,

For that you need to write below line when you hide keyboard.

    editTextObject.setCursorVisible(false);

This will stope cursor to blink.

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
  • the problem is i don't hide the keyboard, the user does it by pressing the back button – the beest Sep 10 '16 at 19:21
  • yepbut you can implement that time keyboard hide function and there you can set cursor visibility as above so when user will click on back button and keyboard will be hide at that time it will call this function and also hide cursor from edittext – Vickyexpert Sep 12 '16 at 04:31
0

As you said, the blinking cursor in the EditText is related to the EditText having focus, but showing or hiding the soft keyboard has no correlation to a View gaining or losing focus. Any View (EditText or otherwise) can be focused independent of whether or not a soft keyboard is showing and there is nothing intrinsic to EditText that would make it behave any differently.

If you want an EditText to lose focus whenever the soft keyboard is hidden, you will need to implement this functionality yourself by listening for changes in the soft keyboard visibility and updating the EditText as a result.

happydude
  • 3,869
  • 2
  • 23
  • 41
  • yes, but i haven't seen any examples of someone hacking up a soft keyboard listener when the activity's `windowSoftInputMode="adjustPan"` – the beest Sep 04 '16 at 18:40
  • I see...Can I ask why it is important to continue using the activity after hiding the keyboard? For example when filling out forms or initiating a search the user would not be compelled to hide the keyboard for any length of time, and if they did they would probably want the cursor retained so that they could easily find the last place they entered information in order to pick up where they left off. – happydude Sep 04 '16 at 19:03
  • well, in order for the user to submit the form they have to hit the button at the bottom of the screen, so the user must close the keyboard to get to it -- the button cannot be scrolled to when the keyboard is up... is there no way for me to clear focus on a view as the soft keyboard is hidden? – the beest Sep 04 '16 at 21:51
  • I am not sure, but I guarantee it will be way easier to just submit the form via the keyboard after filling out the EditText. Leave the button in your layout, but look at TextView's IME options, specifically setImeActionLabel() and setImeOptions(). There are equivalent xml attributes to use in a layout as well. These options allow you to essentially "click" your existing form submittal button directly from the soft keyboard. – happydude Sep 04 '16 at 22:17
  • it's not that simple. the description EditText near the bottom of the layout is optional, so it's impossible to know whether or not to submit the form or go to the description EditText when the user hits the enter key when finished with the EditText right before the description. – the beest Sep 07 '16 at 14:52
0

The only way to know keyboard is disappeared is to override OnglobalLayout and check the height. Based on that event you can "setCursorVisible(false)" on your edit text

surya
  • 607
  • 5
  • 18
  • this doesn't work if the activity has the attribute android:windowSoftInputMode="adjustPan" in the manifest – the beest Sep 07 '16 at 14:52
0

For more information, check this Link.

RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use the layout root
InputMethodManager im = (InputMethodManager)  getSystemService(Service.INPUT_METHOD_SERVICE);

/*
Instantiate and pass a callback
*/
SoftKeyboard softKeyboard;
softKeyboard = new SoftKeyboard(mainLayout, im);
softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
{

@Override
public void onSoftKeyboardHide() 
{
    // Code here
  EditText.clearFocus();
}

@Override
public void onSoftKeyboardShow() 
{
    // Code here
}   
});

/*
Open or close the soft keyboard easily
*/
softKeyboard.openSoftKeyboard();
softKeyboard.closeSoftKeyboard();

/* Prevent memory leaks:
*/
@Override
public void onDestroy()
{
super.onDestroy();
softKeyboard.unRegisterSoftKeyboardCallback();
}
0

try this:

public class EditTextBackEvent extends EditText {

private EditTextImeBackListener mOnImeBack;

public EditTextBackEvent(Context context) {
    super(context);
}

public EditTextBackEvent(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString());
    }
    return super.dispatchKeyEvent(event);
}

public void setOnEditTextImeBackListener(EditTextImeBackListener listener) {
    mOnImeBack = listener;
}

public interface EditTextImeBackListener {
    void onImeBack(EditTextBackEvent ctrl, String text);
}
  }

in your layout:

<yourpackagename.EditTextBackEvent
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

and in your fragment:

edittext.setOnEditTextImeBackListener(new EditTextBackEvent.EditTextImeBackListener()
    {
        @Override
        public void onImeBack(EditTextBackEvent ctrl, String text)
        {
           edittext.clearfocus();
        }
    });
aligur
  • 3,387
  • 3
  • 34
  • 51
-1

Try keeping a view in your layout which is focusable above your editText.

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:focusable="true"
    android:focusableInTouchMode="true" />

This should work as the blank focusable view should catch focus and not your edittext.

Rohan
  • 593
  • 7
  • 22
  • if you would have read the question, you'd know the problem is that `clearFocus` isn't being called, not that it's being called and isn't working – the beest Sep 11 '16 at 16:16