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'