ArtiomLK ArtiomLK - 1 month ago 13
Java Question

Force EditText to lose focus when: some keyboard keys are pressed and when user clicks on something else in the activity

I know this question has been asked in many different ways before, but even though I have looked at many other related questions about EditText focus, I have not found my solution.

Here is what I want to achieve:


  • When the user is done Editing an EditText I want it to loose focus.

  • When the user hits outside the EditText I want such EditText to lose
    focus.

  • Whenever the soft-keyboard hides or is hidden I want EditText
    to lose focus.

  • Whenever the User hits ENTER or BACK ARROW in the soft-keyboard, the EditText focus should be cleared



This is what I currently have:

I have two EditText in an activity, which I will call EditText_1 and EditText_2 for simplicity so that we know which EditText I am talking about.


  1. When the user starts the activity, the EditText_1 would have no focus, a blinking cursor and the soft-keyboard would be hidden. I have already fixed that problem using;

    android:focusableInTouchMode="true"


    and

    android:focusable="true"

  2. After the previous fix in part1, when I start the activity and click on any EditText, it will gain focus, however, when I am done editing such clicked EditText and the soft-keyboard hides, the EditText will not lose focus and the cursor will still be blinking.

    Another example happens when I am editing an EditText and click any other button outside the editText, it will not force EditText to lose focus or hide the keyboard.



The current solution I have is

public void onFocusChange(View v, boolean hasFocus){...}


so that, it will call some method such as force the EditText to lose focus:

EditText.clearFocus();


and do not show that annoying blinking cursor once I know EditText loses its focus:

EditText.setCursorVisible(false);


However, because hitting Done or outside EditText do not force it to lose focus, onFocusChange will not be called all the times(as in the examples provided).

Some of the solutions I cannot accept are:

Set the cursor visibility to false in the XML activity file and never and anywhere change it back to true.

setCursorVisible(false);


I want the cursor to be seen when needed and to be hidden when it is not needed.

Have a button that needs to be clicked by the user so that inside such button all methods needed will be called. I mean, it is not user-friendly at all. Users do not want to be forced to click a button just to hide all focus, blinking cursors...

Here comes the part many of you will tell me, every single of these issues have been solved in different questions. HOWEVER, I have not been able to implement multiple solutions which will do all 3 points I stated before.

Answer

To make editText lose focus when you press outside of the keyboard you can try to setOnTouchListener to the view that is visible when the keyboard is shown. For example, it might be the parent layout, listView, recyclerView or any other significant in size view. In order to do that, just add code below inside of your onCreate method in activity:

findViewById(R.id.loginLayout).setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        usernameEditText.clearFocus();
        passwordEditText.clearFocus();
        return false;
    }
});

To make editText lose focus and/or hide keyboard when pressing some button on keyboard you can use the following code. There is an example of listener for Enter key. You may find all the other keys on official documentation.

yourEditText.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                yourEditText.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
        }
        return false;
    }
});
Comments