NotchScreenTool

repository·master·Indexed 43 days ago

https://github.com/panliming-tuya/notchscreentool

An Android utility for developers to adapt applications for full-screen displays with notches, waterdrop cutouts, and other screen obstructions. It provides the INotchScreen interface to detect notches via hasNotch, configure UI to draw in the notch area using setDisplayInNotch, and retrieve notch dimensions through NotchScreenInfo and NotchSizeCallback.

Tokens
1.2K
Snippets
6
Records
6
Agent score
6%

What's inside NotchScreenTool

  1. Install NotchScreenTool via Gradle

    master

    To use NotchScreenTool in your Android project, you must first add the JitPack repository to your project-level build.gradle file, and then add the library dependency to your module-level build.gradle file.

    // In project-level build.gradle
    allprojects {
        repositories {
            google()
            jcenter()
            maven { url "https://jitpack.io" }
        }
    }
    
    // In module-level build.gradle
    dependencies {
        implementation 'com.github.smarxpan:NotchScreenTool:0.0.1'
    }
  2. Get notch area information

    master

    You can retrieve information about the device's notch (whether it exists and its physical boundaries) by calling getNotchInfo. This method requires an Activity and an INotchScreen.NotchScreenCallback implementation. The callback returns a NotchScreenInfo object containing the notch details.

    NotchScreenManager.getInstance().getNotchInfo(activity, new INotchScreen.NotchScreenCallback() {
        @Override
        public void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {
            Log.i(TAG, "Is this screen notch? " + notchScreenInfo.hasNotch);
            if (notchScreenInfo.hasNotch) {
                for (Rect rect : notchScreenInfo.notchRects) {
                    Log.i(TAG, "notch screen Rect =  " + rect.toShortString());
                }
            }
        }
    });
  3. Allow UI to draw in the notch area

    master

    By default, Android applications do not draw content into the notch/cutout area. To allow your application's UI to extend into the notch area, call setDisplayInNotch(Activity activity) using the NotchScreenManager singleton.

    NotchScreenManager.getInstance().setDisplayInNotch(activity);
  4. Reference: NotchScreenInfo fields

    master

    The INotchScreen.NotchScreenCallback returns a NotchScreenInfo object with the following fields:

    |字段|类型|是否必填|描述|
    |---|---|---|---|
    |hasNotch|boolean|是|是否是刘海屏|
    |notchRects|List|否|屏幕刘海Rect的列表(屏幕可能有多个刘海)<BR>hasNotch属性为false时此字段为null|
  5. Use INotchScreen to detect and handle notch screens

    master

    The INotchScreen interface provides methods to detect if an Android Activity has a notch (such as a cutout or waterdrop display) and to retrieve the specific dimensions of those notches.

    Key Methods

    • hasNotch(Activity activity): Returns true if the provided activity is running on a device with a notch.
    • setDisplayInNotch(Activity activity): Configures the activity to utilize the notch area (useful for immersive modes).
    • getNotchRect(Activity activity, NotchSizeCallback callback): Asynchronously retrieves the bounding rectangles (Rect) of the notch(s) on the screen.

    Callback Interfaces

    • NotchSizeCallback: Receives a List<Rect> representing the notch areas via onResult(List<Rect> notchRects).
    • HasNotchCallback: Receives a boolean via onResult(boolean hasNotch).
    • NotchScreenCallback: Receives a NotchScreenInfo object via onResult(NotchScreenInfo notchScreenInfo).
    // Example: Detecting notch size
    inotchScreen.getNotchRect(activity, new INotchScreen.NotchSizeCallback() {
        @Override
        public void onResult(List<Rect> notchRects) {
            for (Rect rect : notchRects) {
                // Handle each notch rectangle
            }
        }
    });
  6. Access notch screen information via NotchScreenInfo

    master

    The NotchScreenInfo class is a data container used by NotchScreenCallback to provide a complete snapshot of the screen's notch status. It contains:

    • hasNotch (boolean): Indicates if a notch is present.
    • notchRects (List<Rect>): A list of rectangles defining the area occupied by the notch(s). If hasNotch is false, this list is typically empty.
    class NotchScreenInfo {
        public boolean hasNotch;
        public List<Rect> notchRects;
    }