Write an application to demonstrate the required field validation



In this application, we have used one edit text if it will get no input from the user and user click on button then the message will display on the screen. The xml file is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="Enter Any Name" />

<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1" android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/btnValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etName"
android:layout_below="@+id/etName"
android:layout_marginTop="59dp"
android:text="OK" />

</RelativeLayout>

In this layout file, we have used relative layout and used one edit text to take the input and one button to perform action and toast to display message. The java file is as follows:

package com.example.requiredfieldvalidation;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
Button validate;
EditText name;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

validate = (Button) findViewById(R.id.btnValidate);
name = (EditText) findViewById(R.id.etName);

validate.setOnClickListener(new OnClickListener() {

@Override public void onClick(View view) {
// TODO Auto-generated method stub
checkValidation(view);

}
});
}

@SuppressLint("NewApi") public void checkValidation(View view)
{
String getName = name.getText().toString();
if(getName.equals(""))
{
name.setError("Can not blank this field");
}
else if(!getName.isEmpty())
{
Toast.makeText(this, name.getText().toString(), Toast.LENGTH_LONG).show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

In this java file we have used the following packages:

android

Here the class name is MainActivity which extends the Activity class as follows:
android

Now we have to declare some variables as follows:
android

Here we have declared variable validate of button type and variable name of edit text type, after that we have to register these inside the onCreate() methods as follows:
android

Here we have registered the controls which are defined in the xml layout file. After that we have defined the functionality to perform the action on the button by using the setOnClick() method as follows:
android

In the above, we have overloaded the onClick() method and in this we have called a method named checkValidation() that takes view type argument as follows:
android

Inside this method we have used a string variable to hold the data of edit text which is input by the user. After that we have used the getName.equals() method which is used to compare the data with given quotes means null or empty or blank field and return true then if block will execute and edit text will get focus and display the Can not blank this field message; and if this condition fails then we have used another condition for non empty field, here we have used !getName.isEmpty() method, here this method returns true if edit text is not empty then this will block will be execute and inside this block we have used toast to show the message that is input by the user.
The output is follows:
android

When blanked & press button
android

when input and press button