1

In the default activity created by Android Studio, I added android:textIsSelectable="true". Now, if I long-press a word, it shows copy/share/select all, but when I tap elsewhere, it does not get closed. I need to either click the back button or select one item on the menu. How can I make the menu closed when I tap somewhere else?

There was the (probably) same question (although the questioner did not specify that it was a TextView, I think it was), but the only answer was assuming an EditText and talk about dismissing the software keyboard (which is not needed for TextView) and solving the problem by adding a listener to all views other than EditText. Seems kind of a dirty way. Is that the only way?

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

0

In my case I had a structure like this: CoordinatorLayout > ConstraintLayout > TextView. I already had the following attributes on the CoordinatorLayout, to aid in dismissing the keyboard when tapping "outside" any EditTexts:

android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"

But I just found out you can add android:contextClickable="true" to automatically "dismiss" any text selections. Note though that the focus* attributes are still necessary. The contextClickable attribute also seems to exist for at least LinearLayout, RelativeLayout and ConstraintLayout, so I'm pretty sure it's a standard layout feature and you don't require anything special. Now, except for some "unusual" views like ScrollView I can tap pretty much anywhere in the activity to dismiss the text selection. Or more specifically: anywhere within the CoordinatorLayout, but since it's set to match_parent it simply fills out the entire activity.

I'm not sure if there are any side effects of enabling contextClickable, but so far I haven't noticed any odd behaviour. Apparently a "context click" means something similar to right-click on computers. I'm guessing (standard) layouts have a default listener for it, which causes any existing context menus (like the text selection popup) to close and it simply won't open any of its own.

Sahbi
  • 307
  • 2
  • 9