EditText loses focus on text change even though I have not set it to lose focus

183 Views Asked by At

I'm a noob in Android Development, trying to learn it.

The issue is that I have an edit text that appears upon clicking a button and gains focus and also opens up the keypad.

I have used a TextWatcher class to search the Database and reflect changes onto a RecyclerView whenever a change occurs in the text of the editText.

Also there Is a button besides the editText which is used to clear text, lose focus and close the keypad.

The XML of the specific views that have been mentioned except the RecyclerView

         <ImageButton
                android:id="@+id/ComA_search_open_imgBtn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"
                android:background="@color/black"
                android:importantForAccessibility="no"
                android:src="@drawable/ic_search"
                android:visibility="visible"
                tools:ignore="RtlHardcoded,TouchTargetSizeCheck" />

            <EditText
                android:id="@+id/ComA_search_input_editTxt"
                android:layout_width="300dp"
                android:layout_alignParentStart="true"
                android:layout_height="match_parent"
                android:layout_alignEnd="@id/ComA_search_clearNdExit_imgBtn"
                android:hint="@string/search"
                android:inputType="text"
                android:imeOptions="actionSearch"
                android:visibility="gone"
                android:maxLength="100"
                tools:ignore="Autofill,TextFields" />

            <ImageButton
                android:id="@+id/ComA_search_clearNdExit_imgBtn"
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:src="@drawable/red_cross_vector"
                android:background="@drawable/black_round_bg"
                android:paddingHorizontal="5dp"
                android:layout_alignParentEnd="true"
                android:visibility="gone"
                tools:ignore="ContentDescription" />

The Kotlin Code of the Logic

 private fun setUpSearch() {

        searchCommodityBtn = findViewById(R.id.ComA_search_open_imgBtn)
        searchCommodityEditText = findViewById(R.id.ComA_search_input_editTxt)
        searchCancelBtn = findViewById(R.id.ComA_search_clearNdExit_imgBtn)

        inputMethodManager = searchCommodityEditText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

        searchTextMonitor = SearchTextWatcher()

        searchCommodityEditText.addTextChangedListener(searchTextMonitor)

        searchCommodityBtn.setOnClickListener {

            hideSearchAndTitle()

            showEditTxtAndCancelBtn()

            requestFocusAndShow()

        }

        searchCancelBtn.setOnClickListener {

            startBGSequence()

            clearFocusAndHide()

            searchCommodityEditText.text.clear()

            hideEditTxtAndCancelBtn()

            showSearchAndTitle()

        }

        searchCommodityEditText.setOnEditorActionListener { _, actionId, _ ->

            if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                searchQuery = searchCommodityEditText.text.toString()

                setLoadingAdapter()

                searchCommodity = SearchCommoditySequence()

                checkSearchLife()

                searchCommodity.start()

                clearFocusAndHide()

                return@setOnEditorActionListener true
            }

            false

        }

    }

    private fun showEditTxtAndCancelBtn(){

        searchCommodityEditText.visibility = View.VISIBLE
        searchCancelBtn.visibility = View.VISIBLE

    }

    private fun hideEditTxtAndCancelBtn(){

        searchCancelBtn.visibility = View.GONE
        searchCommodityEditText.visibility = View.GONE

    }

    private fun hideSearchAndTitle() {

        title.visibility = View.GONE
        searchCommodityBtn.visibility = View.GONE

    }

    private fun showSearchAndTitle(){

        searchCommodityBtn.visibility = View.VISIBLE
        title.visibility = View.VISIBLE

    }

    private fun requestFocusAndShow(){

        searchCommodityEditText.requestFocus()

        showKeyboard(searchCommodityEditText)

    }

    private fun clearFocusAndHide(){

        searchCommodityEditText.clearFocus()

        hideKeyboard(searchCommodityEditText)

    }

    private fun hideKeyboard(view: View) {

        inputMethodManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)

    }

    private fun showKeyboard(view: View) {

        inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)

    }

//TextWatcher class

inner class SearchTextWatcher : TextWatcher{
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable?) {

            searchQuery = searchCommodityEditText.text.toString()

            searchCommodity = SearchCommoditySequence()

            checkSearchLife()

            searchCommodity.start()

            Log.d("is editText in Focus","${searchCommodityEditText.hasFocus()}")

        }

    }

The issue is that the editText loses focus whenever the text is changes.

I do have the same kind of code for another activity and it works perfectly fine as expected but this doesn't.

I tried running the app after commenting out the searchCommodity.start() and it worked as expected didn't lose focus.

I believe that the starting of this thread causes this issue and it is completely illogical.

0

There are 0 best solutions below