How can I detect tap on the screen?

一笑奈何 提交于 2019-12-05 02:49:21

Detect Screen Tap

I'm answering this for those who just need a simple way to detect a tap on the screen:

  1. Add an android:onClick value to your base/root layout (LinearLayout, RelativeLayout, etc.). You can call it anything you want, I'm naming it screenTapped as an example:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="screenTapped">
    
  2. Add this method to your Activity using the same name you specified for onClick:

    public void screenTapped(View view) {
        // Your code here
    }
    

Now, tapping on the screen will call the method above.

Erik

I'm answering my own question. Thanks to the above @Jason. I have registered onTouchEvent to all views.
It works great.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!