Is there an easy way to achieve this in Android?
Yes, today there is, and it is very simple.
Just use the MaterialButton
in the Material Components library with the app:cornerRadius
attribute.
Something like:
<com.google.android.material.button.MaterialButton
android:text="BUTTON"
app:cornerRadius="8dp"
../>
It is enough to obtain a Button with rounded corners.
You can use one of Material button styles. For example:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
.../>
Also starting from the version 1.1.0 you can also change the shape of your button. Just use the shapeAppearanceOverlay
attribute in the button style:
<style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Button.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">16dp</item>
</style>
Then just use:
<com.google.android.material.button.MaterialButton
style="@style/MyButtonStyle"
.../>
You can also apply the shapeAppearanceOverlay
in the xml layout:
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
.../>
The shapeAppearance
allows also to have different shape and dimension for each corner:
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopLeft">32dp</item>
<item name="cornerSizeBottomLeft">32dp</item>
</style>