• Stars
    star
    1,152
  • Rank 40,278 (Top 0.8 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Android validation library which helps developer boil down the tedious work to three easy steps.

API Maven Central JitPack Build & Test Travis CI Status CircleCI Status Codacy Badge Android Arsenal Android Weekly Trusted By Many Apps AppBrain

AwesomeValidation

Introduction

Implement validation for Android within only 3 steps. Developers should focus on their awesome code, and let the library do the boilerplate. And what's more, this could help keep your layout file clean.

Steps

  1. Declare validation style;
  2. Add validations;
  3. Set a point when to trigger validation.

Sample code

// Step 1: designate a style
AwesomeValidation mAwesomeValidation = new AwesomeValidation(BASIC);
// or
AwesomeValidation mAwesomeValidation = new AwesomeValidation(COLORATION);
mAwesomeValidation.setColor(Color.YELLOW);  // optional, default color is RED if not set
// or
AwesomeValidation mAwesomeValidation = new AwesomeValidation(UNDERLABEL);
mAwesomeValidation.setContext(this);  // mandatory for UNDERLABEL style
// setUnderlabelColor is optional for UNDERLABEL style, by default it's holo_red_light
mAwesomeValidation.setUnderlabelColorByResource(android.R.color.holo_orange_light); // optional for UNDERLABEL style
mAwesomeValidation.setUnderlabelColor(ContextCompat.getColor(this, android.R.color.holo_orange_dark)); // optional for UNDERLABEL style
// or
AwesomeValidation mAwesomeValidation = new AwesomeValidation(TEXT_INPUT_LAYOUT);
mAwesomeValidation.setTextInputLayoutErrorTextAppearance(R.style.TextInputLayoutErrorStyle); // optional, default color is holo_red_light if not set
// by default, it automatically sets focus to the first failed input field after validation is triggered
// you can disable this behavior by
AwesomeValidation.disableAutoFocusOnFirstFailure();

// Step 2: add validations
// support regex string, java.util.regex.Pattern and Guava#Range
// you can pass resource or string
mAwesomeValidation.addValidation(activity, R.id.edt_name, "[a-zA-Z\\s]+", R.string.err_name);
mAwesomeValidation.addValidation(activity, R.id.edt_tel, RegexTemplate.TELEPHONE, R.string.err_tel);
mAwesomeValidation.addValidation(activity, R.id.edt_email, android.util.Patterns.EMAIL_ADDRESS, R.string.err_email);
mAwesomeValidation.addValidation(activity, R.id.edt_year, Range.closed(1900, Calendar.getInstance().get(Calendar.YEAR)), R.string.err_year);
mAwesomeValidation.addValidation(activity, R.id.edt_height, Range.closed(0.0f, 2.72f), R.string.err_height);
// or
mAwesomeValidation.addValidation(editText, "regex", "Error info");

// to validate TextInputLayout, pass the TextInputLayout, not the embedded EditText
AwesomeValidation mAwesomeValidation = new AwesomeValidation(TEXT_INPUT_LAYOUT);
mAwesomeValidation.addValidation(activity, R.id.til_email, Patterns.EMAIL_ADDRESS, R.string.err_email);

// to validate the confirmation of another field
String regexPassword = "(?=.*[a-z])(?=.*[A-Z])(?=.*[\\d])(?=.*[~`!@#\\$%\\^&\\*\\(\\)\\-_\\+=\\{\\}\\[\\]\\|\\;:\"<>,./\\?]).{8,}";
mAwesomeValidation.addValidation(activity, R.id.edt_password, regexPassword, R.string.err_password);
// to validate a confirmation field (don't validate any rule other than confirmation on confirmation field)
mAwesomeValidation.addValidation(activity, R.id.edt_password_confirmation, R.id.edt_password, R.string.err_password_confirmation);

// to validate with a simple custom validator function
mAwesomeValidation.addValidation(activity, R.id.edt_birthday, new SimpleCustomValidation() {
    @Override
    public boolean compare(String input) {
        // check if the age is >= 18
        try {
            Calendar calendarBirthday = Calendar.getInstance();
            Calendar calendarToday = Calendar.getInstance();
            calendarBirthday.setTime(new SimpleDateFormat("dd/MM/yyyy", Locale.US).parse(input));
            int yearOfToday = calendarToday.get(Calendar.YEAR);
            int yearOfBirthday = calendarBirthday.get(Calendar.YEAR);
            if (yearOfToday - yearOfBirthday > 18) {
                return true;
            } else if (yearOfToday - yearOfBirthday == 18) {
                int monthOfToday = calendarToday.get(Calendar.MONTH);
                int monthOfBirthday = calendarBirthday.get(Calendar.MONTH);
                if (monthOfToday > monthOfBirthday) {
                    return true;
                } else if (monthOfToday == monthOfBirthday) {
                    if (calendarToday.get(Calendar.DAY_OF_MONTH) >= calendarBirthday.get(Calendar.DAY_OF_MONTH)) {
                        return true;
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}, R.string.err_birth);

// to validate with your own custom validator function, warn and clear the warning with your way
mAwesomeValidation.addValidation(activity, R.id.spinner_tech_stacks, new CustomValidation() {
    @Override
    public boolean compare(ValidationHolder validationHolder) {
        if (((Spinner) validationHolder.getView()).getSelectedItem().toString().equals("< Please select one >")) {
            return false;
        } else {
            return true;
        }
    }
}, new CustomValidationCallback() {
    @Override
    public void execute(ValidationHolder validationHolder) {
        TextView textViewError = (TextView) ((Spinner) validationHolder.getView()).getSelectedView();
        textViewError.setError(validationHolder.getErrMsg());
        textViewError.setTextColor(Color.RED);
    }
}, new CustomErrorReset() {
    @Override
    public void reset(ValidationHolder validationHolder) {
        TextView textViewError = (TextView) ((Spinner) validationHolder.getView()).getSelectedView();
        textViewError.setError(null);
        textViewError.setTextColor(Color.BLACK);
    }
}, R.string.err_tech_stacks);

// Step 3: set a trigger
findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mAwesomeValidation.validate();
    }
});

// Optional: remove validation failure information
findViewById(R.id.btn_clr).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mAwesomeValidation.clear();
    }
});

Attention

  • It works perfectly with Fragment, but please pay attention to Fragment's lifecycle. You should set the validate() inside Fragment's onActivityCreated instead of onCreateView or any other early stage.

  • UNDERLABEL validation style doesn't support ConstraintLayout at the moment, please use other validation styles. There is an open issue here.

Import as dependency

For Gradle it's easy - just add below to your module's build.gradle (it's available on Maven Central):

dependencies {
    implementation 'com.basgeekball:awesome-validation:4.3'
}

Alternatively, it's also available on JitPack:

  • Add it in your root build.gradle at the end of repositories:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  • Add the dependency
dependencies {
    implementation 'com.github.thyrlian:AwesomeValidation:v4.3'
    // you can also use the short commit hash to get a specific version
    // implementation 'com.github.thyrlian:AwesomeValidation:GIT_COMMIT_HASH'
}

Screenshots

Release guide

  • Update version number in build.gradle, gradle.properties and README
  • Create new git tag: v*.*
  • Make sure a local.properties file which holds the necessary credentials is present under the library directory
  • Run ./gradlew clean build && ./gradlew :library:publishReleasePublicationToSonatypeRepository to generate release file and upload it to Nexus Repository

Stargazers over time

Stargazers over time

License

Copyright (c) 2014-2021 Jing Li. See the LICENSE file for license rights and limitations (MIT).

More Repositories

1

AndroidSDK

🐳 Full-fledged Android SDK Docker Image
Dockerfile
1,252
star
2

ScreenshotsNanny

Android library helps take screenshots for publishing on Google Play Store.
Java
234
star
3

Charles-Proxy-Mobile-Guide

The mobile hackers' guide to Charles Proxy πŸ‘
140
star
4

awesome-asus-tinker-board

A curated list of ASUS Tinker Board resources
63
star
5

AirPdfPrinter

Virtual PDF AirPrint printer
Dockerfile
57
star
6

Captain-ADB

Providing simple web API and view for Android Debug Bridge (adb). Free your imagination, use it as the way you want.
Ruby
51
star
7

namedict

Generate Chinese names for the newborn baby which are also applicable for multilingual pronunciation (English, and maybe Deutsch).
Ruby
41
star
8

SonarOnDocker

🐳 πŸ“‘ Docker way of running SonarQube + any DB
Java
25
star
9

AgileNotifier

Agile Notifier - an easy way of monitoring Agile SW Engineering, including CI (Continuous Integration), SCM (Source Control Management), and ITS (Issue Tracking System).
Ruby
16
star
10

jooi

Convert the results of Infer (static analyzer by Facebook) to JUnit format results.
Ruby
15
star
11

MacManual

Installation and Setup Guide
14
star
12

PermissionMatters

Check your Android application's permission changes
Go
14
star
13

MobileDevicePool

Web UI to manage your mobile devices kingdom.
Ruby
14
star
14

NoNewPermissionForAndroid

Ruby
10
star
15

TechNewsletter

The engineering way of composing a responsive design newsletter email in markup language
Ruby
8
star
16

dotfiles

Shell
4
star
17

Lifecycle

A curated list of lifecycle explanation in illustration
4
star
18

reMarkable

My reMarkable resources
Shell
4
star
19

AppReputation

Ruby gem for retrieving application's ratings and reviews
Ruby
4
star
20

N0L1mIT

No Limit - η„‘η–†
Shell
4
star
21

JunitReportGenerator

Easy and flexible solution to generating JUnit test result report from any format of data.
Ruby
3
star
22

cryonics

A Chrome extension which helps user save all opened tabs and resuscitate them all at once later.
JavaScript
3
star
23

ACES

ACES - A Chrome Extension Scaffold
HTML
2
star
24

GeekGadgets

2
star
25

AndroidSDKPackagesDownloader

Download missing Android SDK packages automatically via Gradle (from dummy project dependencies)
Java
2
star
26

CozmoSDK

Anki’s CozmoSDK
Nix
2
star
27

Kotlinker

Kotlin Docker Image
2
star
28

Smarping

Smart shopping list benefits from machine learning (not yet)
Java
2
star
29

Doraemon

A bot
JavaScript
2
star
30

RegionChanger

Change region (language, IP address) on the go - debug companion
Java
2
star
31

CurriculumVitae

RΓ©sumΓ© template in LaTeX
TeX
2
star
32

GoJourney

My journey of learning Go
1
star
33

thyrlian.github.io

Website for Basgeekball
CSS
1
star
34

PupilProgramming

Teach πŸ‘¦ how to πŸ‘¨β€πŸ’»
Ruby
1
star
35

MCGW

Most Common German Words - a web crawler extracts vocabularies from canonical German news websites
Ruby
1
star
36

EngineeringExcellence

Engineering Excellence Methodology
1
star
37

BuildCompanion

Your companion for build - monitor, notify, analyze
1
star
38

ComicCollection

Load your comic collection to a server
1
star
39

JIRA-Steward

Kotlin
1
star
40

gradleman

A man who helps you with gradle stuff.
Ruby
1
star
41

VideoHub

Play your videos everywhere (on mobile, Chromecast or whatever)
Ruby
1
star
42

LAB

My personal laboratory for trying out and learning about something new to me πŸ‘¨β€πŸ”¬πŸ”¬
Kotlin
1
star
43

OpenStackSwissKnife

A Docker image contains Swiss knife tool set for managing OpenStack
Shell
1
star
44

mind_the_changes

Mind the code changes since last release
Ruby
1
star
45

PersonalizedBulletinBoard

1
star