Henry Thompson
Henry Thompson

Reputation: 2481

Android EditText Long Press Behaviour

I'm having some problems with the EditText in Android. Usually, when a user long clicks on the EditText or double-taps it, the word the cursor is in is highlighted, and the Contextual Actionbar (CAB) for the EditText pops up (on Android 3.0 and later).

My EditText did indeed do this until recently: the issue is that now long-pressing the EditText results in the selected word being "picked up", i.e. an enlargened "ghost" image of the word is picked up, and you can drag and drop it anywhere else in the text where it is inserted. I have not touched the code for the EditText at all. Anyone running Google Chrome (desktop version) can see the type of behaviour I mean if they highlight any text and drag it with their cursor.

I think the issue may have been caused by my device's upgrade to Android 4.2.2 recently. I have looked all over Google for information, but it has turned up nothing. Also, double-tapping the word also does not bring up the CAB as expected - it flashes for a second then goes away - but I do not think this is a related issue.

I really need this behaviour to stop, as my app cannot function without the CAB. So the question is: how can I get the "normal" behaviour back? All behaviour has been seen on a Nexus 4 running Android 4.2.2. All help is much appreciated; thank you very much!

XML Layout code for the EditText:

<EditText
      android:id="@+id/editor_mainText"
      android:layout_width="40dp"
      android:layout_height="40dp"
      android:layout_above="@+id/options_bottom"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/options_top"
      android:background="#FFE7E7E7"
      android:imeOptions="flagNoExtractUi"
      android:inputType="textMultiLine|textNoSuggestions|textVisiblePassword"
      android:padding="8dp"
      android:scrollbars="none"
      android:textCursorDrawable="@null"
      android:textSize="17sp"
      android:typeface="monospace" >

            <requestFocus />
     </EditText>

Upvotes: 2

Views: 2292

Answers (2)

Dwane13
Dwane13

Reputation: 418

I faced almost exact same behavior. I coudnt find any related question on stackoverflow, so will put my answer here. Maby it will be usefull for somebody. So if you ever faced a problem, that after closing copy/paste popup (by clicking anywhere on the screen) you cant select same text again (getting that "ghost" instead) all you have to do is manually disable textIsSelectable attribute and enable it again on view. For that i used this code im my ActivityMain. Poor solution, but its worked for me

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
    if (ev?.action == MotionEvent.ACTION_DOWN) {
        val v = currentFocus
        if (v is TextView || v is TextInputEditText) {
            val outRect = Rect()
            v.getGlobalVisibleRect(outRect)
            if (!outRect.contains(ev.rawX.toInt(), ev.rawY.toInt())) {
                val view = when (v) {
                    is TextView -> v
                    is TextInputEditText -> v
                    else -> null
                }
                view?.setTextIsSelectable(false)
                view?.refreshDrawableState()
                view?.setTextIsSelectable(true)
            }
        }
    }
    return super.dispatchTouchEvent(ev)
}

Upvotes: 0

Henry Thompson
Henry Thompson

Reputation: 2481

Note: My original answer was a bit wrong. I hadn't spotted my careless mistake in the code, but now that I have, here is why it didn't work

I've solved it, but I absolutely cannot explain why this behaviour happened in the first place, and why doing the following made any difference. But anyway, it did.

The EditText had been extended in order to make it "flingable" using a Scroller and VelocityTracker. This involved Overriding onTouchEvent(MotionEvent). But there was a little mistake in the code, where super.onTouchEvent(MotionEvent) would be called twice, as I had accidentally left out one of the break; statements in the switch-case, except during the MotionEvent.ACTION_MOVE event. The app worked fine even with this error all the way up to Android 4.2.1. I hadn't touched the code following the Android 4.2.2 upgrade, but for whatever reason, in the new version of Android, this small error triggered this weird behaviour.

So basically, it was a very basic mistake, and now I have learnt: always make sure to close off a case in a switch-case with a break statement!

Upvotes: 0

Related Questions