Styling android button

If you want to style android button with custom style with background, custom font different enable disable state button colors you can do that using providing custom style to your button below is the example:

inside res/value folder

1st. style for the button 

<style name=“CustomButtonStyle” parent="Widget.AppCompat.Button">

    <item name="android:background">@drawable/button_background.xml</item>

    <item name="android:textSize">16sp</item>

    <item name="android:textColor">@color/black</item>

    <item name="android:fontFamily">@font/font_name</item>

    <item name="fontFamily">@font/font_name</item>

    <item name="textAllCaps">false</item>

</style>



2nd. in background we are supplying selector so we can have different enable / disable state background and text colors

button_background.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/blue” android:state_enabled="true"/>

    <item android:drawable="@drawable/button_with_border” android:state_enabled="false"/>

</selector>



3rd. now you see in above step we are supplying drawable in place of color for disable state because in place of just different color we want border button where we have colored border around button and different background color for that we need to take advantage of shape property

button_with_border.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <solid android:color="@color/white" />

    <stroke

        android:width=“1dp"

        android:color="@color/blue” />

</shape>


In above xml solid color is background and stroke color is the border color around button.

Comments

Popular posts from this blog

Using TabLayout and ViewPager with CollapsingToolbarLayout

Using android BadgeDrawable to show the Badge android

styling android TabLayout material design