androidontouchlistenerwearables

How do I count the number of times I tap the screen with onTouchListener?


I'm using a wearable device and I'm making an app for it. I have a timer and some other things that begin when I click a start button. Thing is the start button occupies some space that makes things look crowded. I used a Wearable Action Drawer but it doesn't look good, so I resorted to using onTouchlistner with the view in which my objects like the timers are placed.

The layout is a FrameLayout. Now when I use the onTouchListner the MotionEvent I detect is the ACTION_DOWN how do I count this happening 2 times in order to start whatever it is I want to start and when it's 4 times I call my AlertDialog?

frameLayout = (FrameLayout)findViewById(R.id.myscreen);
    frameLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            clicked = clicked + 1;
            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                startTimer();
                //Check flow rate
                if(num == 450)
                {
                    try {
                        flow.setText(String.valueOf(num));
                        progressBar.setIndeterminateDrawable(Circle);
                        progressBar.setVisibility(View.VISIBLE);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                }
                else if(num == 150)
                {
                    try {
                        flow.setText(String.valueOf(num));
                        progressBar.setIndeterminateDrawable(Circle);
                        progressBar.setVisibility(View.VISIBLE);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }

                }

            }
            else if(event.getAction() == MotionEvent.ACTION_DOWN && clicked == 4)
            {

                Handler mHandler = new Handler(Looper.getMainLooper());
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        // Your UI updates here
                        AlertMessage();
                    }
                });

            }

            return true;
        }
    });

clicked is an integer initialized by zero I used that when I was using buttons to count my number of clicks. I now want to count the number of taps to my screen, so if 2 taps ---->start if 4 taps -------> call AlertDialog


Solution

  • Move the counter increment logic together with the counter check logic inside the if if(event.getAction() == MotionEvent.ACTION_DOWN) clause.

    frameLayout = (FrameLayout)findViewById(R.id.myscreen);
        frameLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    if(clicked == 4)
                    {
                        Handler mHandler = new Handler(Looper.getMainLooper());
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                // Your UI updates here
                                AlertMessage();
                            }
                        });
                        return true;
                    }
                    clicked = clicked + 1;
                    startTimer();
                    ....
                }    
                return true;
            }
        });