Android Multi-Touch: Two Fingers Tapping Once

For my project I had to add a feature where the user should use two fingers and tap with both of them at the same time in order to show view.  this behaviour is not very common on Android but I had to implement it anyway. So after I did some research,  and tried multiple ideas I ended up using the code I will present in this tutorial.

So, in this tutorial I will show you a solution for tapping a button with 2 fingers at once. Maybe it’s not the best solution to do this, but it’s the only one that I could figure it out. The code it’s simple so… let’s start 🙂

1. Create a new project and call your java class(the one that is generated by Eclipse or other IDE) “MyActivity”.

2.  Go to res – layout – main.xml (for those who use Eclipse this name probably should be changed, but I’m using Intellij Idea and this is how the ide generates it, so I don’t have to change it) and change the default TextView tag with a Button tag like in the code below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            android:id="@+id/button"
            />
</LinearLayout>

3.  Go to MyActivity class and put the following code:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyActivity extends Activity {

    // flag used for knowing if we have tapped with 2 fingers (TRUE) or only with 1 finger (FALSE)
    private boolean mTwoFingersTapped;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                // Only if the OnTouch listener is not activated (mTwoFingersTapped = FALSE) we show the toast for click.
                // Otherwise if the mTwoFingersTapped flag is TRUE, we have to set it to false, so we can use the click
                // for 1 finger.
                if (!mTwoFingersTapped){
                    Toast.makeText(MyActivity.this, "Single tap or click!", 0).show();
                }else {
                    mTwoFingersTapped = false;
                }
            }
        });

        button.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionEvent.getAction();

                switch(action & MotionEvent.ACTION_MASK)
                {
                    case MotionEvent.ACTION_POINTER_DOWN:
                        Toast.makeText(MyActivity.this, " Two Fingers Tapped Once. Yeeeyy :)", 0).show();

                        // set the mTwoFingersTapped flag to TRUE when we tap with 2 fingers at once
                        mTwoFingersTapped = true;
                        break;
                }
                return false;
            }
        });
    }
}

That’s it! Our project is done and ready for testing, but let’s try to look at the code first and try to understand a little how the code works. As you can see we made one button with a listener for click (1 finger tap) and a listener for Touch (in our case 2 fingers tap). By now everything seems alright, but actually it’s not. The OnTouchListener interferes with the OnClickListener. When the OnTouchListener is activated it does it’s job, but also does the job of the OnClickListener. So when the OnTouchListener is activated, the OnClickListener always activates too. So to “repair” this I made the flag called “mTwoFingersTapped” , to track the touch event.

I hope this tutorial to help you and don’t forget, if you like it and you want to support us, you can donate 😉

keyboard_arrow_up
sponsored
Exit mobile version