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.
Copy the JAR file in the libs folder of your project.
|Click to enlarge|
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.|
Our sample XML layout file (activity_main.xml) will have the following code:
Step 5Our main activity will have the following code:
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.|
That's it! Happy New Year guys! :)
For FREE programming tutorials, click the red button below and subscribe! :)