Search code examples
javaandroidcanvasontouch

Can't draw on canvas & listen touch to view simultaneously


My main activity contains a textView. It does run an animation if touch. In the same screen i can draw line (example) with an extending view.

OnTouchEvent from canvas must return true but then onTouch from textView will never get it.

I want that touch screen work for both. How can i do that ?

MainActivity.java

public class MainActivity extends AppCompatActivity {
    TextView tv,lettres;
    TranslateAnimation animation;
    ConstraintLayout constraintLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        tv.setX(100);
        tv.setY(100);
        constraintLayout = findViewById(R.id.cl);
        lettres = new TextView(this);
        constraintLayout.addView(lettres);
        lettres.setVisibility(View.GONE);
        lettres.setText("test");
        tv.setOnTouchListener(touchListener);
    }

    private View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (view == tv){

                        lettres.setX(50);
                        lettres.setY(50);
                        animation = new TranslateAnimation(0,10,0,10);
                        lettres[i].startAnimation(animation[i]);
           }
            return false;
        }
    };
}

activity_main.XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <fr.DrawingView
        android:id="@+id/myDrawingView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

DrawingView.java

public class DrawingView extends View {
    Paint myPaint;
    int debutX=0;
    int debutY=0;
    int finX=0;
    int finY=0;

    public DrawingView(Context context, AttributeSet attributeSet) {
        super(context,attributeSet);
        myPaint = new Paint();
        myPaint.setColor(Color.RED);
        myPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        myPaint.setStrokeWidth(20);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(debutX, debutY, finX, finY, myPaint);
        invalidate();
    }
   @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                debutX = (int) event.getX();
                debutY = (int) event.getY();
                finX = (int) event.getX();
                finY = (int) event.getY();
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                finX = (int) event.getX();
                finY = (int) event.getY();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                invalidate();
                break;
        }
        return true;
    }
}

Solution

  • because your main layout consumes the touch event. and you return true inside onTouchEvent