Tuesday, December 18, 2018

android - Material button example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rootLayout"
        tools:context=".MainActivity">

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Material Default Button"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp"
            android:id="@+id/materialButton"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Outline Button"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            android:id="@+id/materialButton2"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text Button"
            style="@style/Widget.MaterialComponents.Button.TextButton"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton2"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp"
            android:id="@+id/materialButton3"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unelevated Button"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton3"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp"
            android:id="@+id/materialButton4"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Icon Button"
            style="@style/Widget.MaterialComponents.Button.Icon"
            android:id="@+id/materialButton5"
            app:icon="@drawable/ic_action_refresh"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHorizontal_bias="0.495"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton4"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Icon Text Button"
            style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
            android:id="@+id/materialButton6"
            app:icon="@drawable/ic_action_refresh"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton5"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tint Icon Button"
            style="@style/Widget.MaterialComponents.Button.Icon"
            android:id="@+id/materialButton7"
            app:icon="@drawable/ic_action_refresh"
            app:iconTint="#b49e0c"
            app:iconTintMode="multiply"
            app:iconPadding="25dp"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton6"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Colored Button"
            android:id="@+id/materialButton8"
            app:backgroundTint="#bc13a0"
            app:backgroundTintMode="multiply"
            android:padding="10dp"
            android:insetTop="25dp"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton7"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Custom Material Button"
            android:id="@+id/materialButton9"
            app:cornerRadius="10dp"
            app:backgroundTint="#3f974b"
            app:backgroundTintMode="multiply"
            app:rippleColor="#ffee33"
            app:strokeColor="#8edea7"
            app:strokeWidth="2dp"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/materialButton8"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
gradle dependencies

// Material components theme
implementation 'com.google.android.material:material:1.0.0'
More android examples