0

I have a little problem, in my xml I have this:

<LinearLayout
        android:id="@+id/mainView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_margin="10dp" />

        <LinearLayout
            android:id="@+id/fistItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="@string/app_name"
                android:textColor="#fff"
                android:textSize="20sp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/secondItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="@string/app_name"
                android:textColor="#fff"
                android:textSize="20sp" />
        </LinearLayout>
</LinearLayout>

And I have this in my Activity file :

public class MyActivity extends Activity implements OnTouchListener,
        OnClickListener {

    LinearLayout mainView;
    LinearLayout firstItem;
    TextView header;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);

        mainView = (LinearLayout) findViewById(R.id.mainView);
        firstItem = (LinearLayout) findViewById(R.id.fistItem);
        header = (TextView) findViewById(R.id.title);
        mainView.setOnTouchListener(this);
        firstItem.setOnClickListener(this);

    }

    int n = 0;

    @Override
    public void onClick(View v) {
        header.setText("First Item Clicked: " + ++n + " times");
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Toast.makeText(getApplicationContext(), "View Touched",
                    Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}

The problem comes when I click firstItem. I can see how the tittle text change but the Toast doesn't pop-up, but when I click the secondItem it does.

GLopez
  • 183
  • 2
  • 13

2 Answers2

1

In order to raise onClick event for firstItem via onTouch on mainView, you need to return true in your onTouch method. For example:

@Override
public boolean onTouch(View v, MotionEvent event) {
    ...

    return true;  //now it will raise any registered onClick event too
}

When you return false in onTouch, it consumes the touch event and doesn't pass it to subsequent onClick event.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Actually `onClick` is getting called when I click **firstItem**, but not `onTouch` listenr for **mainView**. I think it would work if it have been the opposite – GLopez Aug 16 '13 at 15:47
0

You're missing this

        firstItem.setOnTouchListener(this);

in your onCreate()

donkey
  • 204
  • 1
  • 7
  • This works but, there is no way that a parent layout can handle `onTouch` listener from their childs when this have an `onClick` listener?. – GLopez Aug 16 '13 at 16:10
  • http://stackoverflow.com/questions/12168254/catch-ontouch-event-by-parent-handle-it-and-pass-to-children – donkey Aug 19 '13 at 02:25