Search code examples
androidandroid-edittextfocus

android EditText focus behavior


I thought this would be a simple thing to implement, but apparently not. I have a Fragment with 2 editTexts and 1 TextView. all have custom Drawables for background. Here's the behavior I'm looking for:

  1. when fragment opens, nothing has the focus - that's working
  2. when you tap on either of the edits, it gets the focus, all text is selected, and softKeyboard opens in number mode - that's working too
  3. after entering the new number, when the blue key in the bottom right corner of the softkeyboard is clicked (tab or checkmark symbol), the softkeyboard closes and nothing has the focus - no blinking cursor. - that's what I can't get to work.
    Please help. Kotlin or Java OK

Solution

  • You need to implement this logic (you can upgrade it further if you want, it's just an example)

    Layout:

    ...
    
            <View
                android:id="@+id/f_c_focus_view"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:layout_width="0dp"
                android:layout_height="0dp" />
    
            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:imeOptions="actionDone"
                android:inputType="number" />
    
            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:imeOptions="actionDone"
                android:inputType="number"/>
    ...
    

    In layout you can see hidden 'focus' view, it necessary for interception focus, because if you have an EditText as first component of the view it always will receive the focus. My EditText components have an imeOptions="actionDone" which I will handle in code further, you can have different imeOptions.

    Code:

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
    
        ...
    
            focusView.requestFocus() // intercept focus on view created
    
            editText1.setOnEditorActionListener { v, actionId, _ ->
                when(actionId) {
                    EditorInfo.IME_ACTION_DONE -> { // handle checkmark key press on the keyboard
                        v.clearFocus()
                        hideKeyBoard(v)
                    }
                }
    
                false
            }
    
            editText2.setOnEditorActionListener { v, actionId, _ ->
                when(actionId) {
                    EditorInfo.IME_ACTION_DONE -> {
                        v.clearFocus()
                        hideKeyBoard(v)
                    }
                }
    
                false
            }
    
       ...
    }
    
    private fun hideKeyBoard(v: View) {
        val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(v.windowToken, 0)
    }