What is meaning of boolean value returned from an event-handling method in Android

45,828

Solution 1

If you return true from an ACTION_DOWN event you are interested in the rest of the events in that gesture. A "gesture" in this case means all events until the final ACTION_UP or ACTION_CANCEL. Returning false from an ACTION_DOWN means you do not want the event and other views will have the opportunity to handle it. If you have overlapping views this can be a sibling view. If not it will bubble up to the parent.

Solution 2

From the documentation : http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent)

"True if the listener has consumed the event, false otherwise."

If you return true, the event is processed. If false, it will go to the next layer down.

Solution 3

The boolean value determines whether the event is consumed or not.

Yes you're correct. If you return false, the next listener handles the event. If it returns true, the event is consumed by your listener and not sent to the next method.

Solution 4

All above answer is correct but the result is different is if the View is clickable or not clickable

Example, I have a LinearLayout contains 1 Button and 1 TextView like this

<LinearLayout
    android:id="@+id/linearlayout_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0aa"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp"
        android:text="Button Click"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textview_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp"
        android:text="TextView Click"
        android:textSize="20sp"
        android:background="#e4e4e4"
        />

</LinearLayout>

In Activity, I have code like

class MainActivity : AppCompatActivity() {
    val TAG = "TAG"

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<LinearLayout>(R.id.linearlayout_root).setOnTouchListener { v, event ->
            Log.i(TAG, "LinearLayout onTouch event " + getDisplayAction(event.action))
            false
        }

        findViewById<Button>(R.id.button_click).setOnTouchListener { v, event ->
            Log.i(TAG, "Button onTouch event " + getDisplayAction(event.action))
            false
        }

        findViewById<TextView>(R.id.textview_click).setOnTouchListener { v, event ->
            Log.i(TAG, "TextView onTouch event " + getDisplayAction(event.action))
            false
        }
    }

    private fun getDisplayAction(action: Int): String {
        return when (action) {
            MotionEvent.ACTION_DOWN -> "DOWN"
            MotionEvent.ACTION_MOVE -> "MOVE"
            MotionEvent.ACTION_UP -> "UP"
            MotionEvent.ACTION_CANCEL -> "CANCEL"
            MotionEvent.ACTION_OUTSIDE -> "OUTSIDE"
            else -> "UNKNOWN"
        }
    }
}

Case 1 Linear onTouch return **FALSE**, Button onTouch return **FALSE**, TextView onTouch return **FALSE**

Click on Button

I/TAG: Button onTouch eventDOWN
I/TAG: Button onTouch eventMOVE
I/TAG: Button onTouch eventUP

Click on TextView

TAG: TextView onTouch eventDOWN
TAG: LinearLayout onTouch eventDOWN

Click on LinearLayout

TAG: LinearLayout onTouch eventDOWN

Case 2 Linear onTouch return **FALSE**, Button onTouch return **TRUE**, TextView onTouch return **TRUE**

Click on Button

Similar to case 1

Click on TextView

TAG: TextView onTouch event DOWN
TAG: TextView onTouch event MOVE
TAG: TextView onTouch event UP

Click on LinearLayout

Similar to case 1

Case 3 Linear onTouch return **TRUE**, Button onTouch return **FALSE**, TextView onTouch return **FALSE**

Click on Button

Similar to case 1

Click on TextView

TAG: TextView onTouch event DOWN
TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP

Click on LinearLayout

TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP

Note

  • Default of TextView is not clickable, it will become clickable if we set android:clickable="true" in xml OR when we set textView.setOnClickListener(...)
  • When you debug, event MOVE can call more than my log (it base on how you tap)

Summary

  • onTouch return true or view is clickable , View will receive all onTouchEvent
  • onTouch return false and view is not clickable, view will not receive NEXT onTouchEvent (it's parent may receive it)

Hope it help
DEMO

Solution 5

I lost nearly one day in troubleshooting, still i found out, that my onTouch function is called 2 times when using true and 1 times when using false.

Share:
45,828

Related videos on Youtube

John Wang
Author by

John Wang

Updated on July 08, 2022

Comments

  • John Wang
    John Wang almost 2 years

    In android, most event listener methods return a boolean value. What is that true/false value mean ? what will it result in to the subsequence events ?

    class MyTouchListener implements OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            logView.showEvent(event);
            return true;
        }
    }
    

    Regarding to the above example, if return true in onTouch method,I found every touch event(DOWN,UP,MOVE,etc) has been captured according to my logView. On the contrary,if return false, onely the DOWN event been captured. So it's seemd that return false will prevent the event to propagate. Am I correct ?

    Furthermore, in a OnGestureListener, many methods have to return a boolean value too. Do they have the same meaning ?

  • adamp
    adamp over 13 years
    This is false. true means you consumed the event and want the rest of the events in the gesture - other listeners/views will not receive the events. false means let someone else handle the event. It's actually a bit more specific than that though; see my answer.
  • Falmarri
    Falmarri over 13 years
    How is that not exactly what I said?
  • adamp
    adamp over 13 years
    What you said is reversed. :)
  • ticofab
    ticofab over 10 years
    adamp, is there a way to keep receiving events AND let the events through?
  • adamp
    adamp about 10 years
    @ticofab no, only a parent of the view currently receiving events can intercept future events in the gesture. (Of course, you can always build your own redirect systems in a parent view, but I wouldn't recommend it unless you really know what you're doing. :) )
  • Bhargav Jhaveri
    Bhargav Jhaveri over 9 years
    Could you find out the reason?
  • Bhargav Jhaveri
    Bhargav Jhaveri over 9 years
    @adamp I cannot think of the reason why onTouch is called 2 times when returning true and 1 time only when I return false.
  • doodeec
    doodeec over 9 years
    check the event.getAction() because if you return false on ACTION_DOWN event, ACTION_UP event is ignored by listener
  • Mahantesh M Ambi
    Mahantesh M Ambi over 9 years
    @adamp :I am returning false from ACTION_DOWN but my ACTION_UP is getting triggered and executed.
  • Kai Wang
    Kai Wang about 8 years
    Am I getting this reply wrong or everyone else is... This reply said that return true means tough event is not consumed. But the truth is completely reverse.
  • Mysterious_android
    Mysterious_android almost 5 years
    This should be the answer ! Thank you for such a detailed explanation