Search code examples
androidandroid-viewontouchlistener

Multiple OnTouchListeners


I have made a custom View which has the OnTouchEvent(MotionEvent event) method. This makes the view change color when it is pressed.

I have also implemented an OnTouchListener to my activity and then register the view with it by doing this view.setOnTouchListener(this); This brings up a problem that means that only the activity's OnTouchListener works.

Is there a solution to this?

This is my code after trying the link given in the comments. Sorry for the amount!:

My custom view:

public class DotView extends View implements OnTouchListener {

    int RADIUS = 30;
    int prevRADIUS;

    private Paint paint = new Paint();

    float mTranslateX;
    float mTranslateY;

    int dotX, dotY, color;

    private Activity context = null;

    public void ReservedCanvas(Activity context) {
        super(context); 
        this.context = context;

        this.paint = new Paint();

        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setAntiAlias(true);
    }

    public DotView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setColor(Color.BLACK);

    }

    public void setColor(int color) {
        paint.setColor(color);
        invalidate();
    }

    public int getColor() {
        return paint.getColor();
    }

    public void setRadius(int radius) {
        RADIUS = radius;
        invalidate();
    }

    public int getRadius() {
        return RADIUS;
    }

    public void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        canvas.save();
        canvas.translate(mTranslateX, mTranslateY);
        canvas.drawCircle(0, 0, RADIUS, paint);
        canvas.restore();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int dia = RADIUS * 4; 
        int w = resolveSize(dia, widthMeasureSpec);
        int h = resolveSize(dia, heightMeasureSpec);
        setMeasuredDimension(w, h);
        float radius = Math.min(w, h) / 2F;
        mTranslateX = radius;
        mTranslateY = radius;
    }


    private void setOnTouchFeedback(Boolean bool) {
        // TODO Check this works

        if (bool == true) {

        int rad = getRadius();
        prevRADIUS = rad;
        float touchedRad = (float) (rad * 1.4);
        int rounded = Math.round(touchedRad);
        setRadius(rounded);
        }


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;

        switch (action) {
        case MotionEvent.ACTION_DOWN: {
            this.pointerPressed(event);
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            this.pointerDragged(event);
            break;
        }
        case MotionEvent.ACTION_UP: {
            this.pointerReleased();
            break;
        }
        }

        return true;
    }

    protected void pointerPressed(MotionEvent event) {
        // you can save touch point here!

        setOnTouchFeedback(true);
    }

    protected void pointerDragged(MotionEvent event) {
        // for get x ==> eveevent.getX()
        // for get y ==> eveevent.getY()

        this.repaint();
    }

    protected void pointerReleased() {
    }

    public void repaint() {
        this.invalidate();
    }
}

Solution

  • From the Documentation of the onTouchListener - http://developer.android.com/reference/android/view/View.OnTouchListener.html i think that the Listener needs to return false after showing the toast, so that the onTouchMethod of the View is called.

    have you tried that?