Published on

Using Android Gesture Navigation and Gestures

Authors
  • avatar
    Name
    how-to.digital
    Twitter

Using Android Gesture Navigation and Gestures

In this tutorial, we will explore the Android Gesture Navigation feature and learn how to effectively use gestures in Android applications. Gesture Navigation allows users to navigate through the Android system and applications using intuitive swipe gestures, reducing the reliance on traditional navigation buttons.

Prerequisites

Before we begin, make sure you meet the following prerequisites:

  • Basic knowledge of Android development
  • Android Studio installed on your computer
  • An Android device or emulator with Android 10 (API level 29) or higher

Getting Started

To start using Android Gesture Navigation, follow these steps:

  1. Open your Android project in Android Studio.
  2. Ensure that your project targets Android 10 (API level 29) or higher.
  3. Make sure your device or emulator is running Android 10 or higher.

Configuring Gesture Navigation

To configure Gesture Navigation for your Android application, you need to modify the application's navigation settings. Follow these steps:

  1. Open the MainActivity.java file in your project.
  2. Inside the onCreate() method, add the following code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    View decorView = getWindow().getDecorView();
    int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
    decorView.setSystemUiVisibility(flags);
}

This code enables Gesture Navigation and sets a light navigation bar style.

  1. Run your application on your device or emulator to see the changes.

Implementing Gestures

Once Gesture Navigation is enabled, you can implement various gestures in your Android application. Let's learn how to implement common gestures step by step:

1. Swipe Gesture

A swipe gesture allows users to perform actions by swiping across the screen. Here's how to implement it:

  1. Create a new OnSwipeTouchListener.java file in your project.
  2. Add the following code into the file:
public class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                        result = true;
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                        result = true;
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}
  1. In your MainActivity.java file, inside the onCreate() method, add the following code to enable the swipe gesture:
View view = findViewById(R.id.main_layout);
OnSwipeTouchListener swipeTouchListener = new OnSwipeTouchListener(this) {
    @Override
    public void onSwipeRight() {
        // Add your code for right swipe action
    }
};
view.setOnTouchListener(swipeTouchListener);
  1. Now, when the user swipes right, the onSwipeRight() method will be called, allowing you to perform the desired action. You can implement the same logic for other directions.

2. Pinch Gesture

A pinch gesture allows users to zoom in or out on the screen. Here's how to implement it:

  1. In your MainActivity.java file, inside the onCreate() method, add the following code to enable the pinch gesture:
View view = findViewById(R.id.main_layout);
view.setOnTouchListener(new View.OnTouchListener() {
    private float scaleFactor = 1.0f;
    private ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        scaleGestureDetector.onTouchEvent(event);
        return true;
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scaleFactor *= detector.getScaleFactor();
            scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 10.0f));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);
            return true;
        }
    }
});
  1. Now, when the user performs a pinch gesture, the onScale() method will be called, allowing you to handle the zooming functionality. You can modify the code to suit your specific requirements.

Conclusion

In this tutorial, we learned how to use Android Gesture Navigation and implement common gestures in Android applications. By utilizing Gesture Navigation and gestures, you can enhance the user experience and provide intuitive navigation options in your Android apps. Experiment with different gestures and explore the possibilities of gesture-based interactions in your applications.