1

I have activity with recycleView which included some edit texts.when selecting any edit text keyboard getting the popup. But when I moving recycle view to up keyboard changing to default one.

I found the reason for it that when edit text gets disappear it removed from the recycle view so it lost the focus. Then keyboard changing to default.

Now I need to close keyboard when there is no focus for any edit text when moving recycleview.But I do not know how to get that kind of call back when there is no forcus for the edit texts.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rajitha Perera
  • 1,581
  • 5
  • 26
  • 42
  • 2
    Maybe detect the keyboard hidden/shown like this: https://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device – DataDino Jun 27 '19 at 04:18

1 Answers1

1

I want to suggest you one method of recyclerView called onViewDetachedFromWindow. For example when you scroll the recyclerView, item become invisible and at that moment adapter calls:

override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder is MyViewHolder) {
            holder.checkForFocus()
        }
}

After in viewHolder you may check for focus:

inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bind(item: MyViewItem) {
            itemView.editText.hint = item.title
        }

        fun checkForFocus() {
            if(itemView.editText.hasFocus()){
                callback.hideKeyBoard()
            }
        }
    }
}

var callback: IKeyBoard? = null // initialize it in fragment/activity


interface IKeyBoard {
        fun hideKeyBoard()
}

After that you can implement the callback method in fragment/activity to hide keyboard

I hope that I helped you