Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active April 11, 2018 17:24
Show Gist options
  • Select an option

  • Save gbzarelli/b4d436bc042164e5d0f3a8826856cf60 to your computer and use it in GitHub Desktop.

Select an option

Save gbzarelli/b4d436bc042164e5d0f3a8826856cf60 to your computer and use it in GitHub Desktop.
Android Material Design Button Styles

With the Support Library v7, all the styles are actually already defined and ready to use, for the standard buttons, all of these styles are available:

style="@style/Widget.AppCompat.Button"
style="@style/Widget.AppCompat.Button.Colored"
style="@style/Widget.AppCompat.Button.Borderless"
style="@style/Widget.AppCompat.Button.Borderless.Colored"

Step 1: Define the system colors in your style. (The buttom with above styles use accent color)

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Step 2: Define your style in the buttom

<Button style="@style/Widget.AppCompat.Button.Colored"
[...]
android:text="Button"/>

For a specific button:

If you need to change the style of a specific button, you can define a new style, inheriting one of the parent styles described above. In the example below I just changed the background and font colors:

<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/your_color_buttom</item>
    <item name="android:textColor">@color/your_text_color</item>
</style>
  • Then you just need to apply this new style on the button with:

    android:theme="@style/AppTheme.Button"

  • To set a default button design in a layout, add this line to the styles.xml theme:

    @style/AppTheme.Button

This sets the button style for all the buttons in a layout with a specific theme

Step 1: Use the latest support library

compile 'com.android.support:appcompat-v7:27.1.0'

Step 2: Use AppCompatActivity as your parent Activity class

public class MainActivity extends AppCompatActivity

Step 3: Use app namespace in your layout XML file

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

Step 4: Use AppCompatButton instead of Button

<android.support.v7.widget.AppCompatButton
    android:id="@+id/buttonAwesome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Awesome Button"
    android:textColor="@color/whatever_text_color_you_want"
    app:backgroundTint="@color/whatever_background_color_you_want"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment