I'm following the tips from questions like this to create a button style like suggested on Material Design.
However, I need to change the corner radius and haven't been able to do so by inheriting Widget.AppCompat.Button.Colored
style and setting the radius parameter.
How can I have the same style but with rounded corners?
Try below code Create a drawable file called circular_button.xml and insert the below
Then change the background of the button to this drawable file
If you want a full circle button you can use the below drawable
Also another simple way is wrap it around cardView,Remember to set the layout_width and layout_height of the cardView to wrap_content, also all the needed margin the button will need should be applied to the cardView
Rounded Material Button with Ripple effect
Create a file in drawable folder ripple.xml
Create a file in drawable folder rounded_shape.xml
And on your Button:
Update:
Answer by Gabriele Mariotti below is now better.
Old answer:
You need to inherit that style.
Add into your styles.xml:
Add file drawable/rounded_shape.xml:
And finally in your layout:
Edit: updated answer to use theme's color rather than hardcoded one.
I will tell you my exact solution for this . Inside selector tags, you can put items (functionality of the buttons)
Second item of the selector tag has the opposite behaviour. You can add as much as selector (button behaviour) ADD THIS DRAWABLE XML AS A BACKGROUND OF THE BUTTON android:background="@drawable/this xml"
You can also use the new Material Components for Android library.
Add the dependency to your
build.gradle
:In this case you can use a
MaterialButton
in your layout file:Use
app:cornerRadius
attribute to change the size of corner radius. This will round off the corners with specified dimensions.You can also customize the corners using the
shapeAppearanceOverlay
attribute (it requires version 1.1.0)The official doc is here and all the android specs here.
OLD Support Library:
With the new Support Library 28.0.0, the Design Library now contains the
Material Button
.You can add this button to our layout file with:
You can set the corner radius with this attribute:
app:cornerRadius
: Used to define the radius used for the corners of the button