0

I have this code which doesn't return any errors. However it doesn't work like it's supposed to. I'm trying to launch an application when I swipe up with two fingers but nothing is happening. Can anyone see what the problem with my code?

public class HomeActivity extends Activity {

    ImageButton phoneButton;
    ImageButton contactsButton;
    ImageButton messagesButton;
    ImageButton cameraButton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);

        setTitle("Apps");


        loadApps();
        loadListView();
        addClickListener();
        addphoneButtonListener();
        addmessagesButtonListener();
        addcontactsButtonListener();
        addcameraButtonListener();


//Two-Finger Drag Gesture detection
        RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.mainLayout);
        TextLoggerLayout.setOnTouchListener(
                new RelativeLayout.OnTouchListener(){

                    @Override
                    public boolean onTouch(View v, MotionEvent m) {
                        handleTouch(m);

                        return true;
                    }

                });



    }
    void handleTouch(MotionEvent m){
        //Number of touches
        int pointerCount = m.getPointerCount();
        if(pointerCount == 2){
            int action = m.getActionMasked();

            switch (action)
            {
                case MotionEvent.ACTION_DOWN:
                    Intent i;
                    PackageManager manager = getPackageManager();
                    try {
                        i = manager.getLaunchIntentForPackage("com.google.android.googlequicksearchbox");
                        if (i == null)
                            throw new PackageManager.NameNotFoundException();
                        i.addCategory(Intent.CATEGORY_LAUNCHER);
                        startActivity(i);
                    } catch (PackageManager.NameNotFoundException e) {

                    }
                    break;
                case MotionEvent.ACTION_UP:

                    break;
                case MotionEvent.ACTION_MOVE:

                    break;
                case MotionEvent.ACTION_POINTER_DOWN:

                    break;


            }


        }
        else {
           //do something
        }
    }
user1353517
  • 300
  • 3
  • 10
  • 28
  • I see a couple of issues with your code. The most obvious one is that you should definitely handle the `NameNotFoundException`. At least log it. The second is that you will potentially be launching the application multiple times, because handleTouch will be fired multiple times unless you control it globally. Oh, and with your code *as is* you aren't detecting a swipe. Take a look at [this answer](http://stackoverflow.com/a/14873942/1003528). – Joao Sousa Mar 04 '15 at 13:28

2 Answers2

1

Try debugging handleTouch() to see exactly why its not firing. My guess would be because you have your check on MotionEvent.ACTION_DOWN, which in real world is a bit difficult to happen exactly at the same time for two fingers.

Move the check to MotionEvent.ACTION_MOVE (which is what you want anyway cause you want swipe, not tap) and try again.

Evripidis Drakos
  • 872
  • 1
  • 7
  • 15
0

MotionEvent.ACTION_DOWN means one finger. MotionEvent.ACTION_POINTER_DOWN means secondary touches (eg. two fingers).

So moving your code to case MotionEvent.ACTION_POINTER_DOWN: should solve your issue.

Documentation.

shkschneider
  • 17,833
  • 13
  • 59
  • 112