Simple validate without IF.
Add maven jitpack.io and dependencies in build.gradle (Project) :
// build.gradle project
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// build.gradle app/module
dependencies {
...
implementation 'com.github.gzeinnumer:ValidatorValue:version'
//required for testing
implementation 'com.github.gzeinnumer:DialogAndroid:3.0.0'
implementation 'com.github.gzeinnumer:SimpleMaterialStyle:2.0.0'
}- Type 1
Return Result and use Toast as message if validate fail
btnValidate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strUsername = edUsername.getText().toString();
String strPassword = edPassword.getText().toString();
// If you want to hide default `Toast` use this.
//ValidatorValue.with()
ValidatorValue.with(getApplicationContext())
.addValue(strUsername, "Minimal 5 Character", 5)
.addValue(strPassword, "Minimal 8 Character", 8)
.validateListener(new ValidatorValueResult() {
@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
//do something
}
});
}
});- Type 2
Return Result and use Custom message if validate fail
btnValidate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strUsername = edUsername.getText().toString();
String strPassword = edPassword.getText().toString();
ValidatorValue.with(getApplicationContext())
.addValue(strUsername)
.addValue(strPassword, "Minimal 8 Character", 8)
.validateListener(new ValidatorValueResult() {
@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}, new ValidatorValueMessage() {
@Override
public void onFailed(String msg) {
new InfoDialog(getSupportFragmentManager())
.setDialogType(DialogType.DialogError)
.setTitle("INFO!!!")
.setContent(msg).show();
}
});
}
});You can customize Length and Message for validate and required length
String value = "GZeinNumer";
String msg = "Username Can't Empty";
int minLength = 5;
ValidatorValue.with(getApplicationContext()).addValue(value); // msg = "Required correct value" // minLenth = 1
ValidatorValue.with(getApplicationContext()).addValue(value, msg); // minLenth = 1
ValidatorValue.with(getApplicationContext()).addValue(value, msg, minLength);You can put boolean to
String value = "GZeinNumer";
boolean isValid = value.length()>0;
String msg = "Username Can't Empty";
ValidatorValue.with(getApplicationContext()).addValue(isValid); // msg = "Required correct value"
ValidatorValue.with(getApplicationContext()).addValue(isValid, msg);MainActivity.java & activity_main.xml
- 1.0.0
- First Release
- 1.1.0
- Hide or Display Message
You can sent your constibution to branch open-pull.
Fore More All My Library
Copyright 2021 M. Fadli Zein

