Handle touch outside textview after selectable text menu appears in android

Viewed 201

I have implemented selectable text in android via
android:textIsSelectable="true"

What I now need to do is make the menu disappear when there is touch anywhere other than the text/menu/cursors. How do I achieve this?

1 Answers

Firstly you can follow this link to hide software keyboard/do action after click outside edittext(or any view type you want to achieve Handle outside click

For example

fun setupUI(view: View, action: () -> Unit) {
// Set up touch listener for non-text box views to hide keyboard.
if (view !is EditText && view !is AppCompatEditText) {
    view.setOnTouchListener { _, _ ->
        action.invoke()
        false
    }
}

//If a layout container, iterate over children and seed recursion.
if (view is ViewGroup) {
    for (i in 0 until view.childCount) {
        val innerView: View = view.getChildAt(i)
        setupUI(innerView, action)
    }
}

}

Then in your action clear your rootview focus to dismiss text copy menu: rootView.clearFocus()

Related