I have a RelativeLayout
in which I am adding many TextViews
dynamically. The problem I am facing is, whenever I apply an onTouch listener to TextViews
individually, it detects touches but when i add touch to my relative layout, it never responds.
This code detects touch events just fine:
TextView tv = new TextView(this);
tv.setText(values[i]);
Drawable d = getResources().getDrawable(R.drawable.level_small_corners);
tv.setClickable(true);
tv.setId(i+1);
tv.setTextSize(18);
tv.setOnTouchListener(cellTouch);
But when I add all these TextViews in myRelativeLayout:
myRelativeLayout.setOnTouchListener(cellTouch);
Now, the onTouchListener never gets called. Why is that so?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black">
.
.
.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/levelFirstLayout"
android:layout_marginBottom="70dp" >
<RelativeLayou
android:id="@+id/wordsRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
</RelativeLayout>
</ScrollView>
.
.
.
</RelativeLayout>
Yes, insofar as it will be deprecated when the v26 edition of the support libraries are released in production form, later in 2017.
Intercept touch events in a ViewGroup. The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of a ViewGroup , including on the surface of its children.
RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).
In myRelativeLayout.xml add:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
This worked for me:
yourRelativeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
//gesture detector to detect swipe.
gestureDetector.onTouchEvent(arg1);
return true;//always return true to consume event
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With