I'm from iOS development and new in Android app making, something looks really strange to me in Android, why EditText stay focused when keyboard is hidden ??

I've tried to set a OnFocusChangeListener on my EditText but this is not working when the keyboard hide, the listener isn't called.

I've also tried to detect keyboard hiding with a onChangeListener but it doesn't work.. (only with hard keyboard apparently).

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // not called when keyboard hides
        }
    });

I've been looking for a while and I only found answer for stopping focus at first launch but that's not what I'm looking for..

Thanks

6 Answers

0
Dinesh Shingadiya On

Try this:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
1
notTdar On

Okay, kind of this

  private void setUpEtxFocusListener() {
    etx.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                if ((LOG_DEBUG)) Log.d(TAG, "etx : GOT Focus");

            } else {
                if ((LOG_DEBUG)) Log.d(TAG, "etx : LOST Focus");

            }
        }
    });

and when clicked outside, etx will lose focus and you hide the keyboard

 //this will trigger etx  setOnFocusChangeListener -  onFocus change() - NO FOCUS clause
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if (v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                v.clearFocus();
               // just an utility class to hide.
                UtilExtra.hideKeyboard(this);
            }
        }
    }
    return super.dispatchTouchEvent(event);
}
0
amirhesni On

you can simply use this code and your focus will be gone

editText.clearFocus();
0
Suren On

To catch keyboard's open/close event use this code:

    //...
    private int layoutSize = 0;
    private boolean isKeyboardVisible = false;
    //...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setKeyboardOpenListener();
    }
    //...
    private void setKeyboardOpenListener() {
            View activityRootView = findViewById(android.R.id.content);
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
                if (activityRootView.getHeight() + Util.getStatusBarHeight(this) >= layoutSize) {
                    layoutSize = activityRootView.getHeight();
                    if (isKeyboardVisible) {
                        isKeyboardVisible = false;
                        onKeyboardClose();
                    }
                } else {
                    if (!isKeyboardVisible) {
                        isKeyboardVisible = true;
                        onKeyboardOpen();
                    }
                }
            });
        }
    //...
    public static int getStatusBarHeight(Context context) {
            int result = 0;
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId);
            }
            return result;
        }
-1
kgsharathkumar On

Use editText.clearFocus() and editText.setCursorVisible(false) both methods, It may helps you.

0
Taseer Ahmad On

Recently I solved exact this problem with a good library. It handles Keyboard visibility event.

Add this to your App level Gradle file.

dependencies {
implementation'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.3.0'
}

Enable keyboard event:

KeyboardVisibilityEvent.setEventListener(
    getActivity(),
    new KeyboardVisibilityEventListener() {
        @Override
        public void onVisibilityChanged(boolean isOpen) {
            if (isOpen)
                  //Keyboard is showing.
            else
               yourEditText.clearFocus()
               yourEditText.setCursorVisible(false) //Only if the cursor keeps blinking else no need use it.
                 //Keyboard is now closed.
        }
    });

Sadly, there is no 'proper' way to handle keyboard visibility event. All you get are hacks to detect it.