Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Library as Dependency
Open build.gradle file in Gradle Scripts and add new library as below:
implementation 'com.mobsandgeeks:android-saripaar:2.0.3'
Add Strings
Open res\values\strings.xml file and add new string as below:
<resources>
<string name="app_name">Learn Android with Real Apps</string>
<string name="i_agree">I agree to the terms of use</string>
<string name="username">Username</string>
<string name="password">Password</string>
<string name="confirm_password">Confirm Password</string>
<string name="email">Email</string>
<string name="website">Website</string>
<string name="age">Age</string>
<string name="phone">Phone</string>
<string name="save">Save</string>
<string name="username_already_exists">Username already exists</string>
</resources>
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/username"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword" />
<EditText
android:id="@+id/editTextConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/confirm_password"
android:inputType="textPassword" />
<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/phone"
android:inputType="phone" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/age"
android:inputType="number" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextWebsite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/website"
android:inputType="textPersonName" />
<CheckBox
android:id="@+id/checkBoxAgree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/i_agree" />
<Button
android:id="@+id/buttonSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/save" />
</LinearLayout>
Main Activity Class
Add code to MainActivity.java in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.mobsandgeeks.saripaar.QuickRule;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.Checked;
import com.mobsandgeeks.saripaar.annotation.ConfirmPassword;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.Length;
import com.mobsandgeeks.saripaar.annotation.Max;
import com.mobsandgeeks.saripaar.annotation.Min;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Pattern;
import com.mobsandgeeks.saripaar.annotation.Url;
import java.util.List;
public class MainActivity extends AppCompatActivity implements Validator.ValidationListener {
@NotEmpty
@Length(min = 3, max = 10)
private EditText editTextUsername;
@NotEmpty
@Password
@Pattern(regex = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})")
private EditText editTextPassword;
@ConfirmPassword
private EditText editTextConfirmPassword;
@NotEmpty
@Pattern(regex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$")
private EditText editTextPhone;
@Min(18)
@Max(120)
private EditText editTextAge;
@NotEmpty
@Email
private EditText editTextEmail;
@Url
private EditText editTextWebsite;
@Checked
private CheckBox checkBoxAgree;
private Button buttonSave;
private Validator validator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
validator = new Validator(this);
validator.setValidationListener(this);
validator.put(editTextUsername, new QuickRule<EditText>() {
@Override
public boolean isValid(EditText view) {
EditText editText = (EditText) view;
if (editText.getText().toString().equalsIgnoreCase("abc")) {
return false;
}
return true;
}
@Override
public String getMessage(Context context) {
return getText(R.string.username_already_exists).toString();
}
});
}
private void initView() {
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
editTextConfirmPassword = findViewById(R.id.editTextConfirmPassword);
editTextAge = findViewById(R.id.editTextAge);
editTextPhone = findViewById(R.id.editTextPhone);
editTextEmail = findViewById(R.id.editTextEmail);
editTextWebsite = findViewById(R.id.editTextWebsite);
checkBoxAgree = findViewById(R.id.checkBoxAgree);
buttonSave = findViewById(R.id.buttonSave);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonSave_onClick(view);
}
});
}
private void buttonSave_onClick(View view) {
validator.validate();
}
@Override
public void onValidationSucceeded() {
Toast.makeText(this, "We got it right!", Toast.LENGTH_SHORT).show();
}
@Override
public void onValidationFailed(List<ValidationError> errors) {
for (ValidationError error : errors) {
View view = error.getView();
String message = error.getCollatedErrorMessage(this);
// Display error messages
if (view instanceof EditText) {
((EditText) view).setError(message);
} else {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
}
}
Structure of Project
Run App
Load Main Activity
Test with Invalid Data
Test with Valid Data