Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnTouchListener doesn't work with relative layout

Tags:

java

android

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>
like image 265
Muhammad Umar Avatar asked Jun 24 '12 20:06

Muhammad Umar


People also ask

Is Relative layout deprecated in Android?

Yes, insofar as it will be deprecated when the v26 edition of the support libraries are released in production form, later in 2017.

How to intercept touch event android?

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.

What is RelativeLayout?

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).


2 Answers

In myRelativeLayout.xml add:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
like image 111
ρяσѕρєя K Avatar answered Sep 21 '22 08:09

ρяσѕρєя K


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
    }
});
like image 24
Ramakant Avatar answered Sep 18 '22 08:09

Ramakant