Exploring Android 12: Rounded Corner API

M Farhan Majid
3 min readMar 15, 2022

--

Android 12 introduces Rounded Corner API that enables you to get the properties of a screen’s rounded corners, such as its center and its radius. As you can see in the image above, with this API you app can be made aware of the screen’s rounded corner and avoid truncating the UI elements.

In this article, we will create a simple application that implements this new API together. Let’s do it!

Where’s the Source Code?

Step-by-Step Explanation

Follow the steps provided below to make this application.

1. Open Android Studio. Create new project with “Empty Activity” option.

2. We are going to make an app that demonstrates a button being truncated by its rounded corners. For that, we need to update our theme first. Open res/values/themes.xml and change the parent of the base application theme to the NoActionBar variant. There are several of these variants. One of them can be seen in the code snippet below:

3. Next, we will build our layout. This will be very simple. We only need a layout with a button at the corner of it like shown in the image below.

To make this layout, open your activity_main.xml file and copy paste the code snippet below:

4. Lastly, we are going to update our MainActivity.kt file. You can copy-paste the code snippet below directly. But here’s the gist of what’s going on here. First, notice how we override onWindowFocusChanged method. This is because we use View.getLocationInWindow method which is not available on onCreateView. Second, notice how we need to hide the system bar (status bar especially) so that we can demonstrate the truncated button more clearly. And lastly but most importantly, the usage of WindowInsets.getRoundedCorner to obtain the center and radius of the screen’s top-right rounded corner. By obtaining the corner’s properties and the button’s coordinates, we can determine whether the button is truncated or not. If it is, then we add a margin to the button so that it is no longer truncated.

5. That’s it! Now you can run your app and you should see the result as shown in the image below.

The result of using Rounded Corner API. The left image shows how the button is truncated by the screen’s corner. The right image shows how the button avoids being truncated by calculating the corner’s radius & center obtained through Rounded Corner API.

As always, thanks for reading!

Want to learn more about Android 12?

Check out all of our articles from “Exploring Android 12" series here:

Exploring Android 12

9 stories

--

--