Blog purpose for android basic example for android app developer. any query please my contact

Wednesday, 15 April 2020

form validation in android example

In this tutorial, today learn form validation in android example. there is validation using a textbox, edit textbox all the android palates.
follow the step by step form validation in android example.

form validation in android example function using the button click event.

  buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chekvalidation();
            }
        });
    }


Today we will learn about Android Form Validation. In any app input validation is a very important thing to do. Input Validation eliminates the errors that can be done by user while giving inputs to our app. For example if we want to get the user’s email we can check the entered email is a valid email or not before storing it inside the database. So lets start our Android Form Validation tutorial.


follow the source code of form validation in android example.


1.MainActivity.java:

package com.akash.checkvalidation;


import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

EditText editTextfirstName,editTextlastname,editTextPassword,editTextMobile;

Button buttonSubmit;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        editTextfirstName=(EditText)findViewById(R.id.editTextfirstName);

        editTextlastname=(EditText)findViewById(R.id.editTextlastname);

        editTextPassword=(EditText)findViewById(R.id.editTextPassword);

        editTextMobile=(EditText)findViewById(R.id.editTextMobile);

        buttonSubmit=(Button)findViewById(R.id.buttonSubmit);


        buttonSubmit.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                chekvalidation();

            }

        });

    }


    public void chekvalidation(){



        if(editTextfirstName.getText().toString().equals("")){


            Toast.makeText(this,"Please Enter First Name",Toast.LENGTH_SHORT).show();

        }


        if(editTextlastname.getText().toString().equals("")){


            Toast.makeText(this,"Please Enter Last Name",Toast.LENGTH_SHORT).show();

        }

        if(editTextPassword.getText().toString().equals("")){


            Toast.makeText(this,"Please Enter Password ",Toast.LENGTH_SHORT).show();

        }


        if(editTextMobile.getText().toString().equals("")){


            Toast.makeText(this,"Please Enter Mobile Number",Toast.LENGTH_SHORT).show();

        }




    }

}



2.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextfirstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter First Name"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editTextlastname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Last Name"
        android:inputType="textPersonName" />


    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Password"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editTextMobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Mobile Number"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/buttonSubmit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>


3.Output:

form-validation-in-android-example



form-validation-in-android-example







No comments:

Post a Comment