Edittext Picker Library📋

Behind the scene story of removing hectic code of java to create multi-functional edittext library

Ali Azaz Alam
AndroidPub

--

In past whenever I got project to build Survey app that contains lots of sections and in each have bunch of questions including multiple validations to hectic functionalities. Mostly I adopted old rule, follow the same path in those projects and work get done. It was become hectic like wasting time to write same LOC (lines of code)again and again, even I can achieve this functionality in smarter way. I started working on library that could provide Edittext with muti functional attributes by which we can easily get the result in smarter way.

Introduction

Edittext library, it’ll totally eradicate the way we did in past, helps the developers to boost their development in respect to time and quality.

This library provides multiple functionalities, includes:

  • Empty Edittext checking
  • Masking Edittext
  • Pattern Edittext checking
  • Range Edittext checking

Past Procedure

Old pattern of validation

Lot of LOC😨? Like for range checking, first we can write LOC for empty checking after that check the range. Same procedure for pattern. The another scenario when we don’t want to validate any edittext for this we’ve to write some more LOC for validation. Hooo😡 write container of code to achieve single operation.

But now, its over😳

Implementation EdittextPicker??

Gradle

In project.gradle, add this code it in root build.gradle at the end of repositories:

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

Now, add the dependency in app.gradle:

Get the latest release from here📱

dependencies {
implementation 'com.github.AliAzaz:Edittext-Library:1.0.2'
}

Now, sync the project👍

XML and JAVA

Moving forward about the xml attributes and its respective code in java that we’ve to follow.

XML work-side:

Required:
When you want to check, is the edittext empty or not? Use Required attribute takes boolean value means true or false. By default it’s true.

Sample code snippet:

<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtBoxReq"
---------
app:required="true" />

Masking:
If you’ve some web development background then you mostly used mask in different ways, like for date, phone no or for NIC etc. I introduced it in this library so you can get the same flavor.

Sample code snippet:

<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtMask"
---------
app:mask="+92(###)####-###" />

Note: The “#” is specify where the user input will go

Type:
It’ll provide two options that’s range and equal and both of it use for different operations.

Firstly, talk about range:

When you want to validate the edittext that it can only accept specific range for example height (from 10–100) means lower bound limit is 10 and upper bound is 100. So, you can easily achieve through defining in this way.

Sample code snippet:

<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtBoxRange"
---------
android:inputType="number"
app:maxValue="100"
app:minValue="10"
app:type="range" />

Waooo 👌 It’s soo easy….😎

Wait!!Wait!! two attributes left that’s defaultValue and pattern attribute, as it’s not mandatory to implement both of it but defining of these attribute is to provide user a penalty that if the value is not coming in range then define default value or to provide pattern to match decimal range, the library will also validate it.

Sample code snippet:

<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtBoxRange"
---------
android:hint="##.##"
android:inputType="number"
app:defaultValue="999"
app:maxValue="100"
app:minValue="10"
app:pattern="^(\\d{2,2}\\.\\d{2,2})$"
app:type="range" />

Note: The minValue, maxValue and defaultValue takes both datatypes that’s int and float.

Moving forward to another option of type that is equal:

In most of the cases we requires the input need to match some alphanumeric pattern like “ABCD-1234”. Simply pass the pattern to pattern attribute in xml. But there is a trick in this type. Whenever you can pass type=equal, it’s mandatory to define defaultValue, the functionality provide by this attribute is primarily same as I already defined in range type that if pattern can’t match then library will match it with defaultValue.

Sample code snippet:

<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtBoxDefault"
---------
app:defaultValue="null"
app:pattern="[^0-9]{2,4}[0-9]{3,5}"
app:type="equal" />

Note: The defaultValue takes all primitive datatypes.

Soon, I’ll also upload an article about HOW TO DEFINE PATTERN in ANDROID 🤞💥

JAVA work-side:

We implemented all the coding in xml now move forward to java side. We are going to implement these codes in the onClick event of Submit Form Button so, it can validate the EditTextPicker components.

Library provides three functions for three different operations. The functions return boolean value either true or false.

Sample code snippet:

New pattern of validation

OOhhh👌 all done… Great you implemented library successfully.

Combined All Components CODE Snippet

View XML file

<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtBoxRange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Range of number 5-10"
android:inputType="number"
android:textColor="@color/colorPrimary"
app:defaultValue="999"
app:maxValue="100"
app:minValue="10"
app:required="true"
app:type="range"/>
<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtBoxRangeMaskPat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="##.##"
android:inputType="number"
app:defaultValue="0"
app:mask="##.##"
app:maxValue="40"
app:minValue="0.5"
app:pattern="^(\\d{2,2}\\.\\d{2,2})$"
app:required="true"
app:type="range"/>
<!--Accept first 2-4 characters
then 3-5 numbers-->
<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtBoxDefault"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="follow pattern or default values 'null'"
app:defaultValue="null"
app:pattern="[^0-9]{2,4}[0-9]{3,5}"
app:required="false"
app:type="equal"/>
<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="##-##-####"
android:inputType="number"
app:mask="##-##-####"
app:required="false"/>
<com.edittextpicker.aliazaz.EditTextPicker
android:id="@+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="+92(###)####-###"
android:inputType="phone"
app:mask="+92(###)####-###"
app:required="true" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SUBMIT" />

View JAVA file

private boolean validateComponents() {     if (!txtBoxRange.isRangeTextValidate())
return false;
if (!txtBoxRangeMaskPat.isRangeTextValidate())
return false;
if (!txtBoxDefault.isTextEqualToPattern())
return false;
if (!txtDate.isEmptyTextBox())
return false;
return txtPhone.isEmptyTextBox();
}
private void clearFields() {
txtBoxRange.setText(null);
txtBoxRangeMaskPat.setText(null);
txtBoxDefault.setText(null);
txtMask.setText(null);
}
//Call this listener in OnCreatebtnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (validateComponents()) {
Toast.makeText(MainActivity.this, "Successfully submitted!!", Toast.LENGTH_SHORT).show();
clearFields();
}
}
});

OUTPUT

Do you want to HELP this library?

I need your help to build this library much more productive. Give Star ⭐ ️and Fork this library to contribute in it. I would appreciate your contribution.

Thanks for spending your precious time in reading this article. It’s my first published android Library. If you liked it then Claps 👏 multiple times to say Thanks and helps others by referring it specially to those who are currently working on survey types of apps or others. Please make it possible to successful this library.😊

Connect with me on my socials and become my friend Medium, Twitter, Github and LinkedIn.

--

--

Ali Azaz Alam
AndroidPub

@AndroidAppsDeveloper, @OpensourceContributor, @Writer @Researcher