Giter Site home page Giter Site logo

ialokim / android-phone-field Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lamudi-gmbh/android-phone-field

3.0 2.0 5.0 24.77 MB

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

License: Apache License 2.0

Java 94.69% JavaScript 5.31%

android-phone-field's Introduction

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:

  • PhoneEditText : includes an EditText alongside the flags spinner
  • PhoneInputLayout : includes a TextInputLayout from the androidx.* (before known as support) library alongside the flags spinner

Features

  • Displays the correct country flag if the user enters a valid international phone number, with complete support even for the complex NANP area codes which all start with +1
  • Allows the user to choose the country manually and only enter a national phone number
  • Allows you to choose a default country, which the field will change to automatically when the field is cleared
  • Formats the phone number according to the currently chosen country with whitespaces, dashes and parentheses
  • Validates the phone number, allows to set a custom error
  • Returns the valid phone number including the country code
  • Full internationalization with the countries names in every language provided by Android

Usage

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

  • If not already present, add jitpack to your root build.gradle at the end of repositories:
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
  • In your module's gradle file add the following dependency
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:

  • hint: Sets a string as hint displayed when field is empty
  • defaultCountry: Set the country that should be automatically selected when field is empty. Be sure to use the two letter ISO 3166 format
  • autoFill: Whether the international country code should be automatically inserted on picking a country, defaults to false
  • autoFormat: Whether the phone number should be displayed automatically while typing, defaults to false

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:

  • This library provides two implementations of PhoneField using EditText and TextInputLayout
  • This library allows users to extend the functionality and use custom layouts if needed to match the application theme
  • This library uses a static list of countries generated from the countries.json file in the raw resources
  • This library allows to format phone numbers on the fly
  • This library provides full support even for complicated international phone prefixes
  • This library has full i18n support for every language provided by the Android system

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

android-phone-field's People

Contributors

ialmetwally avatar ialokim avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

android-phone-field's Issues

Problem in xml preview

With the original version of the library or this fork, I have a problem during xml preview but not when I launch my app directly on a phone. It's annoying because if I put this customview in a scrollview I can't scroll in preview.

It's in this method com.github.ialokim.phonefield.CountriesAdapter.getView

This is the stackStrace : java.lang.NullPointerException at android.content.res.Resources_Delegate.getDrawable(Resources_Delegate.java:189) at android.content.res.Resources.getDrawable(Resources.java:827) at android.content.Context.getDrawable(Context.java:635) at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:463) at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203) at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191) at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102) at androidx.appcompat.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:86) at androidx.appcompat.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94) at com.github.ialokim.phonefield.CountriesAdapter.getView(Unknown Source) at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:204) at android.widget.Spinner.onMeasure(Spinner.java:602) at androidx.appcompat.widget.AppCompatSpinner.onMeasure_Original(AppCompatSpinner.java:421) at androidx.appcompat.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535) at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1187) at android.widget.LinearLayout.onMeasure(LinearLayout.java:706) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535) at android.widget.LinearLayout.measureVertical(LinearLayout.java:825) at android.widget.LinearLayout.onMeasure(LinearLayout.java:704) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535) at android.widget.LinearLayout.measureVertical(LinearLayout.java:825) at android.widget.LinearLayout.onMeasure(LinearLayout.java:704) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1308) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.widget.ScrollView.onMeasure(ScrollView.java:350) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) at android.view.View.measure_Original(View.java:23169) at android.view.View_Delegate.measure(View_Delegate.java:80) at android.view.View.measure(View.java:23133) at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:604) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:343) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:385) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:509) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$4(RenderTask.java:638) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

Support night mode?

Hello,

Does this library support night mode?
IF yes how can we implement it and if no what should we do?

Because in dark mode we have as shown in the image below

image

Country selection in dropdown when number is entered with country code

I recently switched to your version of library to finally resolve issue with updating the phone library and everything else related.

But I missing the functionality when country will be re-selected in the drop down when user enters the different country code.

In the video above I would expect UK will be selected in drop down when user enters +44:

Kapture 2020-11-17 at 10 29 23

Cursor issue

I'm not sure if it is Android 11 or something else. Please check the attached video:

Kapture 2020-11-17 at 10 29 23

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.