17

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>

6 Answers 6

18

In myRelativeLayout.xml add:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
1
  • 2
    I have tried this already. It doesn't effect. Look at the additions i have made Jun 25, 2012 at 0:36
15

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
    }
});
7
tv.setClickable(true);

is causing layout, not to fire touch event. Tried removing this.

2
  • 1
    but then wouldn't it mean that the textview won't be able to be focused by touching it? Sep 8, 2013 at 10:02
  • How come setting Clickable disable disables touches, I thought this would enable it.
    – mskw
    Sep 7, 2014 at 17:03
3

It may that something in your view is "on top" of myRelativeLayout. If it is, it's getting the touch event first. The worst part about that is that the default handling of events is to do nothing with the event and then consume it.

One solution would be to add this code onto whatever components of your display are "above" the View (or Layout) that SHOULD handle the event:

someView.setOnTouchListener (new View.OnTouchListener()
{
    @Override
    public boolean onTouch (View v, MotionEvent event)
    {
        return false;
    }
});

The key (obviously) is to return false. When you do that, the event will not be consumed, but rather passed "down" to something in your relative layout -- hopefully somewhere you want it to go.

0
1

I know this is not the solution for your problem, but yours was the closest one to my issue, so maybe this will help somebody else.

Even though it seems like that in the designer, the layout is not "screen wide" in reality with some settings.

Width wrapped, No tap event in padding/layout

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

Or this, No tap event in padding/layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dip"
            android:layout_height="fill_parent"

With match parent, Tap events are firing

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
0

You have to set to your RelativeLayout view this attribute to true: android:clickable="true"

1
  • 1
    It didn't work because the android:clickable switch refers to the use of a clickListener, rather than a touchListener.
    – Rich
    Sep 28, 2012 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.