Saturday 7 May 2016

Create Button with Rounded Corners

Rounded corners buttons are very common for most of the applications. For this, it is more useful if you define a resource file in drawable folder for rounded corners. Define a custome style for button in styles file. Textcolor and background of button depends on your choice. You simply need to replace textcolor and background as per your needs. If you need only one button with rounded corners then you don't need to define style. Otherwise, for multiple buttons style will be very useful.

1. Create a resource rounded_corners.xml layout for rounded corners in @drawable.

This is rounded_corners_button.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="10dp"
        android:shape="rectangle" >

        <!-- This is background color-->
        <solid android:color="#ffffff"/>
        <corners android:radius="5dp"/>
    </shape>
</resources>

2. Now, in styles file, define a custom style for a button with rounded corners. This style can be used for multiple Buttons of your application. Here is style for the button.

<style name="CutomButton" parent="android:style/Widget.Button" >
   <item name="android:layout_width">wrap_content</item>
   <item name="android:layout_height">wrap_content</item>
   <item name="android:textColor">#34bdab</item></item>  
   <item name="android:textSize">20sp</item></item>                   <item name="android:background">@drawable/rounded_corners_button</item>    
 </style>

3. Finally, in your activity layout, create a button and use above mentioned style for button as below:

<Button
             android:id="@+id/btn_signup"
             style="@style/CustomButton"
             android:text="@string/signup"
             android:layout_gravity="center_horizontal"/>

That's all you need for a rounded corner button.

RCPlease don't hesitate to contact me.
Thank you



No comments:

Post a Comment