Git Product home page Git Product logo

phonenumberkit's Introduction

PhoneNumberKit

Android Kotlin library to parse and format international phone numbers. Based on Google's libphonenumber library.

Features

Features
โ˜Ž๏ธ Validate, normalize and extract the elements of any phone number string.
๐ŸŽฏ Convert raw phone number to formatted phone number string.
๐Ÿ” Automatically detects country flag of the phone number.
๐Ÿ”– Country code selection bottom sheet.
๐Ÿ“Œ Convert country codes to country names and vice versa.
๐Ÿ‡น๐Ÿ‡ท Get country flag icon for given iso2 code.

Usage

Create a phoneNumberKit instance and attach it to an editTextLayout. That's all you have to do.

val phoneNumberKit = PhoneNumberKit(this) // Requires context

phoneNumberKit.attachToInput(textField, 1)

To setup with country code selection bottom sheet

phoneNumberKit.setupCountryPicker(this) // Requires activity

To get an example phone number for given iso2 code

val exampleNumber = phoneNumberKit.getExampleNumber("tr")

To parse raw text to phone number and receive country code, national number

val parsedNumber = phoneNumberKit.parsePhoneNumber(
    number = "1266120000",
    defaultRegion = "us"
)

parsedNumber?.nationalNumber
parsedNumber?.countryCode
parsedNumber?.numberOfLeadingZeros

To convert raw text to formatted phone number string

val formattedNumber = phoneNumberKit.formatPhoneNumber(
    number = "1266120000",
    defaultRegion = "us"
)

To receive a country flag icon for given iso2 code

val flag = phoneNumberKit.getFlagIcon("ca")

To receive country name or iso2 code from given country code

val country = phoneNumberKit.getCountry(90)

Usage with Custom Item Layout

Add your custom item layout resource as a parameter

phoneNumberKit.setupCountryPicker(this, R.layout.my_item_layout, searchEnabled = true)

You need to use below view ids in your layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingVertical="12dp"
    android:paddingHorizontal="18dp"
    android:clickable="true"
    android:focusable="true"
    android:background="?android:attr/selectableItemBackground">

    <ImageView
        android:id="@+id/imageViewFlag"
        android:layout_width="22dp"
        android:layout_height="22dp" />

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:singleLine="true"
        android:maxLines="1"
        android:ellipsize="end"
        android:textSize="16sp"
        android:textColor="#232425" />

    <TextView
        android:id="@+id/textViewCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="#838383" />

</LinearLayout>

Demo

Country Code Picker Format Example Format Example

Installation

Step 1. Add the JitPack repository to your build file

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

Step 2. Add the dependency

dependencies {
    implementation 'com.github.ibrahimsn98:PhoneNumberKit:1.5'
}

Checklist

  • Search for country codes
  • Custom list item layout support
  • Better performance with coroutines
  • Phone number validation indicator
  • Dark theme
  • Tests

Conception

License

PhoneNumberKit is available under the Apache license. See the LICENSE file for more info.

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.