Gather Touches from MainActivity

72 Views Asked by At

I want to gather all the possible touches from my entire screen. I implemented a

@Override
   public boolean onTouchEvent(MotionEvent e)
    {
       double x = e.getX();
       double y = e.getY();
    }

But now I added a TableView to the "scene" and when I click on it the above method is not being called because the TableView is swallowing the Touches. http://i.gyazo.com/d671768c45a0d9576d6ba3b822dfa85d.png

Everything around the yellow area is clickable but the yellow area itself not. I want it to be clickable as well, basically I want that all the time the WHOLE screen area is clickable so I can store touch positions. How to do it?

EDIT: The onTouchEvent method is in MainActivity class. Here is the activity_main.xml

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:shrinkColumns="*"
            android:stretchColumns="*" >

            <TableRow android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/column1"
                    android:text="tapcount"
                    android:layout_height="wrap_content"
                    />
            </TableRow>

        </TableLayout>
    </ScrollView>
    </LinearLayout>
2

There are 2 best solutions below

0
On

cover whole view with your own transparent, like this:

<RelativeLayout>
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
            <!--your content inside-->
    </LinearLayout>
    <View
        android:id="@+id/cover"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
</RelativeLayout>

then use your code for View with id=cover and return always false in your onTouchEvent as in doc:

Returns True if the event was handled, false otherwise.

with false hardcoded MotionEvent will be delivered to views below/under

2
On

in your onCreate() method try implementing something like this

View contentView = (View) findViewById(R.id.linearLayout1); //Your layout
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){

                    Toast.makeText(getApplicationContext(), event.getX() + " " + event.getY(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });