Download Saripaar Library from gitHub
Add Saripaar Library Project to your project, (same method of adding actionbarsherlock to your project)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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="18dp"
android:layout_marginTop="42dp"
android:text="Old Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/old_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/old_passwd"
android:layout_marginTop="44dp"
android:text="New Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/new_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textView2"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/new_passwd"
android:layout_marginTop="42dp"
android:text="Confirm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/confirm_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView3"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="@+id/change_password_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/confirm_passwd"
android:layout_marginLeft="39dp"
android:layout_marginTop="55dp"
android:text="Change Password" />
</RelativeLayout>
ConfirmPasswordActivity.java
package com.rajeshvijayakumar.confirmpassword;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;
import com.mobsandgeeks.saripaar.annotation.ConfirmPassword;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.TextRule;
public class ConfirmPasswordActivity extends Activity implements
ValidationListener {
@Required(order = 1)
@TextRule(order = 2, minLength = 6, message = "Enter a New Password Correctly")
private EditText mOldPasswordEditText;
@Required(order = 3)
@Password(order = 4, message = "Enter a Valid Password")
@TextRule(order = 5, minLength = 6, message = "Enter a New Password Correctly")
private EditText mNewPasswordEditText;
@Required(order = 6)
@ConfirmPassword(order = 7, message = "Repeat a New Password Correctly")
private EditText mConfirmNewPasswordEditText;
private Button mChangePasswordButton;
private Validator mChangePasswordValidator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOldPasswordEditText = (EditText) findViewById(R.id.old_passwd);
mNewPasswordEditText = (EditText) findViewById(R.id.new_passwd);
mConfirmNewPasswordEditText = (EditText) findViewById(R.id.confirm_passwd);
mChangePasswordButton = (Button) findViewById(R.id.change_password_button);
mChangePasswordButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mChangePasswordValidator.validate();
}
});
mChangePasswordValidator = new Validator(this);
mChangePasswordValidator.setValidationListener(this);
}
@Override
public void onFailure(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
failedView.requestFocus();
((EditText) failedView).setError(message);
} else {
Toast.makeText(this, "Password Not Changed", Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onSuccess() {
Toast.makeText(this, "SuccessFully Password Changed",
Toast.LENGTH_SHORT).show();
}
@Override
public void onValidationCancelled() {
// TODO Auto-generated method stub
}
@Override
public void preValidation() {
// TODO Auto-generated method stub
}
}
Output :
Source Code : Download this Example Here
Thanks for this great library. It worked always with activities. But now I want to use fragments instead of activities but this library is not working with fragments. The success method is always invoked. Is this library compatible with Fragments?
ReplyDeleteYes, It is compatible with Fragments too. Thank You
Delete