How to get RoundedCorners to work with latest Android

35 views Asked by At

My phone (a Samsung Galaxy S21) has rounded corners on its display screen. I want to add padding to my View so that some text doesn't get cut off by the rounded corners. I was using this code, which worked and did return a nonzero value:-

    private int getCornerAllowance() {
        // return 0 if this Android version doesn't do rounded corners
        int i = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            Display disp = getDisplay();
            // the corner radii are normally all the same
            // but we find the largert one just in case
            RoundedCorner rc = disp.getRoundedCorner(
                RoundedCorner.POSITION_BOTTOM_LEFT);
            int j = rc.getRadius();
            if (j > i) { i = j; }
            rc = disp.getRoundedCorner(
                RoundedCorner.POSITION_BOTTOM_RIGHT);
            j = rc.getRadius();
            if (j > i) { i = j; }
            rc = disp.getRoundedCorner(
                RoundedCorner.POSITION_TOP_LEFT);
            j = rc.getRadius();
            if (j > i) { i = j; }
            rc = disp.getRoundedCorner(
                RoundedCorner.POSITION_TOP_RIGHT);
            j = rc.getRadius();
            if (j > i) { i = j; }
        }
        return i;
    }

and I called it like this

        mResults = new ListView(this);
        int p = getCornerAllowance() * 3 / 10;
        mResults.setPadding(p, 0, p, p);

but after a recent android operating system upgrade, it crashes because getRoundedCorner now returns null.

In case the RoundedCorner objects were no longer getting initialised until the view got attached to a window, I tried this instead

... implementsView.OnAttachStateChangeListener ...

    private int getCornerAllowance() {
        // return 0 if this Android version doesn't do rounded corners
        int i = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            WindowInsets insets = m_container.getRootWindowInsets();
            if (insets != null) {
                // the corner radii are normally all the same
                // but we find the largest one just in case
                RoundedCorner rc = insets.getRoundedCorner(
                        RoundedCorner.POSITION_BOTTOM_LEFT);
                if (rc != null) {
                    int j = rc.getRadius();
                    if (j > i) {
                        i = j;
                    }
                }
                rc = insets.getRoundedCorner(
                        RoundedCorner.POSITION_BOTTOM_RIGHT);
                if (rc != null) {
                    int j = rc.getRadius();
                    if (j > i) {
                        i = j;
                    }
                }
                rc = insets.getRoundedCorner(
                        RoundedCorner.POSITION_TOP_LEFT);
                if (rc != null) {
                    int j = rc.getRadius();
                    if (j > i) {
                        i = j;
                    }
                }
                rc = insets.getRoundedCorner(
                        RoundedCorner.POSITION_TOP_RIGHT);
                if (rc != null) {
                    int j = rc.getRadius();
                    if (j > i) {
                        i = j;
                    }
                }
            }
        }
        return i;
    }
    public void onViewAttachedToWindow(@NonNull View view) {
        int p = getCornerAllowance() * 3 / 10;
        mResults.setPadding(p,0,p,p);
    }

but all the RoundedCorner objects are still null. What am I doing wrong here? Does anyone have code to get rounded corner radius on current versions of Android?

The full code is available here.

0

There are 0 answers