Wed. Feb 28th, 2024

Validation is a crucial aspect to ensure that user inputs are correct and meet the required criteria. Below is an example of how you can perform basic validation for a simple form with an EditText field in Kotlin

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Validations"
         />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:id="@+id/name"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Mobile"
        android:id="@+id/mobile"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email"
        android:id="@+id/email"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:id="@+id/password"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/valid"

        android:text="Validate"/>

</LinearLayout>
</layout>

MainActivity.kt

package com.androindian.validtion

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.androindian.validtion.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    var binding: ActivityMainBinding?=null
    var isAllFieldsChecked=false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= DataBindingUtil.setContentView(this,R.layout.activity_main)

        binding?.valid?.setOnClickListener {
            isAllFieldsChecked=CheckAllFileds()

            if(isAllFieldsChecked){
                Toast.makeText(this@MainActivity,"Valid",Toast.LENGTH_LONG).show()
            }

        }
    }

    private fun CheckAllFileds(): Boolean {
        if(binding?.name?.length()==0){
            binding?.name?.setError("Name Could not be empty")
            return false
        }
        if(binding?.mobile?.length()==0){
            binding?.mobile?.setError("Mobile Could not be empty")
            return false
        }else if(binding?.mobile?.length()!! <=10){
            binding?.mobile?.setError("Mobile nUmber must 10 digits")
            return false
        }
        if(binding?.email?.length()==0){
            binding?.email?.setError("Email Could not be empty")
            return false
        }
        if(binding?.password?.length()==0){
            binding?.password?.setError("Password Could not be empty")
            return false
        }

        return true
    }
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *