I'm developing an Android
application and I'm trying to capture OnTouch
events
in my whole screen. All my activities
will have a header with two buttons
and then a body that's going to change.
In the Activity
I'm testing right now the body is a ListView
, so the Activity
has:
-Two Buttons
at the top of the screen.
-ListView
under those two buttons.
I want to capture onTouchEvents
in the whole screen. I tried setting an OnTouchListener
to my root RelativeLayout
and set clickable=false and focusable=false to all the other views, but it's not working: the onTouch event is only triggered when I click the first button.
This is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
tools:context=".MainActivity" >
<include
layout="@layout/header"
android:clickable="false"
android:focusable="false" />
<ListView
android:id="@+id/lvLocations"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/header_layout"
android:clickable="false"
android:focusable="false" />
</RelativeLayout>
This is the content of @layout/header:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:weightSum="5" >
<Button
android:id="@+id/homeButton"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_marginRight="5dp"
android:layout_weight="4"
android:clickable="false"
android:focusable="false"
android:onClick="Home"
android:text="@string/home" />
<ImageButton
android:id="@+id/speechButton"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:onClick="ClickMediaButton"
android:src="@drawable/micro" />
</LinearLayout>
And this is the code I'm using:
findViewById(R.id.root).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("MACT", "TOUCH!");
}
});
Like I said, my log shows TOUCH only when the homeButton is clicked. Why is that? What am I doing wrong?
Thank you!