-1

Hi I'm new to android studio and I have an app that draws some shapes. Currently when I tap the screen once the app deletes the last shape drawn.

However, I need it to delete the shapes only when the user clicks on the top left 1/16 of the canvas followed by the bottom right 1/16 of the canvas?

This is what I tried.Is there something wrong with my getWidth and getHeight?

This is my onViewCreated

 public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getWidth=view.getWidth();
    getHeight=view.getHeight();

OnsingleTap confirmed method

 public boolean onSingleTapConfirmed(MotionEvent e) {
        Cursor cursor = getLastShape();
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }
        int x1 = (int) e.getX();
        int  y1 = (int) e.getY();

        if((x1<1/16*getWidth) &&(y1<1/16 * getHeight)){
            int x2 = (int) e.getX();
            int  y2 = (int) e.getY();
            if((x2>=getWidth-1/16*getWidth) && (y2>=getHeight-1/16*getHeight)) {
                resolver.delete(Shape.CONTENT_URI, "_id=" + cursor.getInt(cursor.getColumnIndex(Shape.ID)), null);
            }

        }

     return true;



}
Sook Lim
  • 541
  • 6
  • 28

1 Answers1

0

You can use the below code to find Single and Double tap on the specific view.

public class GestureSingleDoubleTap extends AppCompatActivity {

TextView txtTap;
GestureDetector gestureDetector;

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

    onInit();
}

private void onInit() {

    txtTap = findViewById(R.id.txtTap);
    gestureDetector = new GestureDetector(this, new GestureListener());

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

}


private class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Toast.makeText(GestureSingleDoubleTap.this, "Double Tap Occurred", Toast.LENGTH_SHORT).show();
        return true;
    }

    // event when single tap occurs
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Toast.makeText(GestureSingleDoubleTap.this, "Single Tap Occurred", Toast.LENGTH_SHORT).show();
        return super.onSingleTapConfirmed(e);
    }
}

}

And the xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.example.GestureSingleDoubleTap">

    <TextView
      android:id="@+id/txtTap"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:gravity="center"
      android:padding="20dp"
      android:text="Tap me Single or Double" />

</RelativeLayout>
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21