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>
gradle dependencies

// Material components
implementation 'com.google.android.material:material:1.6.1'

android kotlin - TextInputLayout and TextInputEditText example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.os.Bundle
import android.util.Patterns
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.transition.TransitionManager
import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout


class MainActivity : AppCompatActivity() {
    lateinit var rootLayout:ConstraintLayout
    lateinit var layoutEmail:TextInputLayout
    lateinit var layoutPassword:TextInputLayout
    lateinit var etEmail:TextInputEditText
    lateinit var etPassword:TextInputEditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val context:MainActivity = this

        // get the widgets reference from XML layout
        rootLayout = findViewById(R.id.rootLayout)
        layoutEmail = findViewById(R.id.layoutEmail)
        etEmail = findViewById(R.id.etEmail)
        layoutPassword = findViewById(R.id.layoutPassword)
        etPassword = findViewById(R.id.etPassword)
        val btnSignin = findViewById<MaterialButton>(R.id.btnSignin)


        // Button click listener
        btnSignin.setOnClickListener {
            if (isValidForm()){
                Toast.makeText(
                    context,
                    "Login success",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }



    // Custom method to validate form inputted data
    private fun isValidForm():Boolean{
        var isValid = true


        val email = etEmail.text.toString().trim()
        val password = etPassword.text.toString().trim()


        TransitionManager.beginDelayedTransition(rootLayout)
        if (!email.isValidEmail()){
            layoutEmail.isErrorEnabled = true
            layoutEmail.error = "input your email"
            isValid = false
        }else{
            layoutEmail.isErrorEnabled = false
        }


        if (password.isEmpty()){
            layoutPassword.isErrorEnabled = true
            layoutPassword.error = "Input password"
            isValid = false
        }else{
            layoutPassword.isErrorEnabled = false
        }

        return isValid
    }
}



// Extension function to validate email address
fun String.isValidEmail(): Boolean
        = this.isNotEmpty() &&
        Patterns.EMAIL_ADDRESS.matcher(this).matches()
activity_main.xml

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

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layoutEmail"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Email" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layoutPassword"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layoutEmail">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textPassword" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnSignin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Signin"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layoutPassword" />

</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies[add]

// Material components
implementation 'com.google.android.material:material:1.6.1'