Home

Awesome

android-phone-field

A small UI library that allows you to create phone fields with corresponding country flags which format and validate the phone number using libphonenumber from google.

Sample App

The library provides two different fields:

Features

Usage

You can easily add the library to your project using jitpack.io:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
dependencies {
    implementation 'com.github.ialokim:android-phone-field:0.2.3'
}

In your layout you can use the PhoneInputLayout

<com.github.ialokim.phonefield.PhoneInputLayout
     android:id="@+id/phone_input_layout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

or the PhoneEditText

 <com.github.ialokim.phonefield.PhoneEditText
     android:id="@+id/edit_text"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

Both support the following xml attributes:

All of these properties can also be set within your Java code. Please refer to the sample app for some examples.

Customization

In case the default style doesn't match your app styles, you can extend the PhoneInputLayout, or PhoneEditText and provide your own xml, but keep in mind that you have to provide a valid xml file with at least an EditText (tag = phone_edit_text) and Spinner (tag = flag_spinner), otherwise the library will throw an IllegalStateException.

You can also create your own custom view by extending the abstract PhoneField directly.

Countries generation

For better performance and to avoid using json data and then parse it to be used in the library, a simple nodejs is used to convert the countries.json file in raw/countries-generator/ into a plain java utility class that has a two-level static list of countries.

The generation script works as follows:

node gen.js

or

./gen.js

Motivation

This is probably not the the first library with the same purpose, but this one is different for the following reasons:

Attributions

  1. Inspired by intl-tel-input for jQuery and IntlPhoneInput
  2. Flag images from flags
  3. Original country data from mledoze's World countries in JSON, CSV and XML which was highly modified and which is then used to generate a plain Java file
  4. Formatting/validation using libphonenumber
  5. Forked from lamudi-gmbh, to add further features