Android libphonenumber Example

Photo of Mike Dalisay
Modified Saturday, January 5, 2013
by - @ninjazhai
Today I want to show you a simple example code on how to use a very useful library called libphonenumber, a phone number handling library from Google. It can validate the correct number format for your country. In this example we're using PH (Philippines) as country code. It is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich). Phone number validation is essential for your app so that you can get useful and sound phone numbers from your loyal users. Any verification code, messages or greetings that you want to send them will have lower chances of being missed.

Example of a valid cellphone number.

You can download the code here:

Online demo here.

Step 1

Download the latest JAR file here. As of this writing, I got libphonenumber-5.2v1.5.jar

Step 2

Copy the JAR file in the libs folder of your project.

Android libphonenumber Example
Click to enlarge

Step 3

Add the JAR on your project. Go to Properties > Java Build Path > Libraries Tab > Click Add Jars.. Button > Browse the JAR file on you libs and click OK. This is what it should look like:

Click to enlarge.

Step 4

Our sample XML layout file (activity_main.xml) will have the following code:

<RelativeLayout xmlns:android=""
   android:layout_height="match_parent" >

       android:inputType="phone" >

        <requestFocus />

       android:text="Validate Phone Number" />


Step 5

Our main activity will have the following code:

package com.example.libphonenumber;


import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        try {
            // so that we can get the inputted phone number
            final EditText et = (EditText) findViewById(;

            View.OnClickListener handler = new View.OnClickListener() {
                public void onClick(View v) {
                    switch (v.getId()) {

                    // clicking the validate button

                        // get the inputted phone number
                        String phoneNumber = et.getText().toString();
                        // On our country, people are used to typing 7 (landline) or 11 (cellphone) digit numbers
                        // To make it 7 digit numbers valid, I have to prepend "02"
                        if (phoneNumber.length() == 7) {
                            phoneNumber = "02" + phoneNumber;

                        // Use the library's functions
                        PhoneNumberUtil phoneUtil = PhoneNumberUtil
                        PhoneNumber phNumberProto = null;

                        try {

                            // I set the default region to PH (Philippines)
                            // You can find your country code here
                            phNumberProto = phoneUtil.parse(phoneNumber, "PH");

                        } catch (NumberParseException e) {
                            // if there's any error
                                    .println("NumberParseException was thrown: "
                                            + e.toString());

                        // check if the number is valid
                        boolean isValid = phoneUtil

                        if (isValid) {

                            // get the valid number's international format
                            String internationalFormat = phoneUtil.format(

                                    "Phone number VALID: " + internationalFormat,

                        } else {

                            // prompt the user when the number is invalid
                                    "Phone number is INVALID: " + phoneNumber,




            // we will set the listeners

        } catch (NullPointerException e) {
        } catch (Exception e) {



Sample Output: (I felt like my phone is too small so I tested it on my tablet haha!)

An example of a valid land line number.

Example of an invalid land line number.

Example of an invalid cellphone number.
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.