For API 21+, Use Clip Views
Rounded outline clipping was added to the View
class in API 21. See this training doc or this reference for more info.
This in-built feature makes rounded corners very easy to implement. It works on any view or layout and supports proper clipping.
Here's What To Do:
android:background="@drawable/round_outline"
setClipToOutline(true)
The documentation used to say that you can set android:clipToOutline="true"
the XML, but this bug is now finally resolved and the documentation now correctly states that you can only do this in code.
What It Looks Like:
Special Note About ImageViews
setClipToOutline()
only works when the View's background is set to a shape drawable. If this background shape exists, View treats the background's outline as the borders for clipping and shadowing purposes.
This means that if you want to round the corners on an ImageView with setClipToOutline()
, your image must come from android:src
instead of android:background
(since background is used for the rounded shape). If you MUST use background to set your image instead of src, you can use this nested views workaround: