Search code examples
javaandroidgesturegesturedetector

Detect single tap on top left of screen


What i'm looking to do is detect when the user has touched a certain part of the screen, the top left corner. I've looked though the documentation

developer.android.GestureDetector and developer.android.GestureDetector.SimpleOnGestureListener

I though of using onSingleTap() but have no idea how it would detect where it was touched. Any help of ideas would be appreciated. Thanks!


Solution

  • You can put a space widget on your layout and size it and place it where you want. Then in your Java code, you can use a plain setOnClickListener.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
      <Space
          android:id="@+id/top_corner_space"
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"/>
    
    </RelativeLayout>
    

    Then in your class:

    Space space = (Space) findViewById(R.id.top_corner_space);
    space.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
        }
    });