I want to make the corners of a button
round. Is there an easy way to achieve this in Android?
style button with icon
<Button
android:id="@+id/buttonVisaProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:background="@drawable/shape"
android:onClick="visaProgress"
android:drawableTop="@drawable/ic_1468863158_double_loop"
android:padding="10dp"
android:text="Visa Progress"
android:textColor="@android:color/white" />
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="14dp" />
<gradient
android:angle="45"
android:centerColor="#1FA8D1"
android:centerX="35%"
android:endColor="#060d96"
android:startColor="#0e7e1d"
android:type="linear" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="270dp"
android:height="60dp" />
<stroke
android:width="3dp"
android:color="#000000" />
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#F7941D">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#F7941D" />
<corners android:radius="10dp" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
<Button
android:background="@drawable/button_background"
android:id="@+id/myBtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="My Button" />
If you want something like this
here is the code.
1.Create a xml file in your drawable folder like mybutton.xml and paste the following markup:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
2.Now use this drawable for the background of your view. If the view is button then something like this:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/mybutton"
android:text="Buttons" />
Simple way i found out was to make a new xml file in the drawable folder and then point the buttons background to that xml file. heres the code i used:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>
</shape>
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>
Create rounded_btn.xml file in Drawable folder...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/#FFFFFF"/>
<stroke android:width="1dp"
android:color="@color/#000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="5dip" android:bottomLeftRadius="5dip"
android:topLeftRadius="5dip" android:topRightRadius="5dip"/>
</shape>
and use this.xml file as a button background
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_btn"
android:text="Test" />