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

Viewed 46117

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 ?

6 Answers

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

Related