• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to Have Different onTouchEvents for Different Views

qwisatz

Lurker
I have a setOnTouchListener for 2 different views, a button with d=button and a textView with Id=text:

Code:
text.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent ev) {
        return true;
    }
});

button.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent ev) {
        return true;
    }
});

Outside Oncreate I override the onTouchEvent to do something:

Code:
@Override
public boolean onTouchEvent(MotionEvent event) {.....

When I touch textView it does whats in the onTouchEvent(MotionEvent event) {.....
When I touch button it does the same thing that's in TouchEvent(MotionEvent event) {.....

Is there anyway I could have the button.setOnTouchListener do something different. then what's happening when I touch "text".

Something like if text.setOnTouchListener then do this
@override
public boolean onTouchEvent(MotionEvent event) {.....Some code from touches

and if its button.setOnTouchListener then do this
Something like if text.setOnTouchListener then do this
@override
public boolean onTouchEvent(MotionEvent event) {.....Different code from touches

The full main Activity code is like this:
Code:
package com.example.qwisatz.drawing;

import android.content.ClipData;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView text;
    Button button;
    Button button2;
    private float mDownX;
    private float mDownY;
    private final float SCROLL_THRESHOLD = 20;
    private boolean isOnClick;
    double startTime, deltaTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.text);
        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);

        text.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent ev) {
                return true;
            }
        });

        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent ev) {
                return true;
            }
        });

        button2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent ev) {
                return true;
            }
        });
    }


   
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("onTouchEvent","test");
        String action = "";

        switch (event.getAction()) {
            //TOUCHES SCREEN
            case MotionEvent.ACTION_DOWN:
                action = "ACTION_DOWN";
                //WHEN TOUCHED mCurrentBox IS SETTED TO BE A NEW BOX WHERE ITS ORIGIN
                //BOX OBJECT TAKES IN A pointF ORIGIN BOX(PointF origin)
                mDownX = event.getX();
                mDownY = event.getY();
                isOnClick = true;
               text.setText("NO MOVEMENT");
                Log.i("down","isOnClick = true");
                startTime = System.currentTimeMillis();
                break;
            //MOVING ON THE SCREEN
            case MotionEvent.ACTION_MOVE:
                    action = "ACTION_MOVE";
                if(Math.abs(mDownX - event.getX()) > SCROLL_THRESHOLD || Math.abs(mDownY - event.getY()) > SCROLL_THRESHOLD) {
                    Log.i("move", "isOnClick = false");
                    ClipData data = ClipData.newPlainText("", "");
                    //CREATES AFTER SHADOWY AFTERIMAGE OF BUTTON
                    View.DragShadowBuilder shadow = new View.DragShadowBuilder(text);
                    text.startDrag(data, shadow, null, 0);
                    text.setText("MOVEMENT");
                    isOnClick = false;
                }
                break;
            //LIFTING OFF THE SCREEN
            case MotionEvent.ACTION_UP:
                action = "ACTION_UP";
                if (isOnClick) {
                    Log.i("Up", "onClick ");
                    //TODO onClick code
                    deltaTime = (System.currentTimeMillis() - startTime);
                    if(deltaTime < 1000){
                        text.setText("Short ");
                    }else{
                        text.setText("Long");
                    }
                }
                break;
            //VIEW INTERCEPTED THE TOUCH EVENT  //VIEW IS A BUTTON/TEXTVIEW ETC...
            case MotionEvent.ACTION_CANCEL:
                action = "ACTION_CANCEL";
                break;
        }
        return true;
    }
}

The full code is a bit messy so I tried my best to simply the problem in my question but there's basically a bunch of things happening it the onTouch that I wanted to code something different for each view.
 
Back
Top Bottom