An easy approach on how to create your country code picker for the edit text.

Overview

Country-Code-Picker

Original Article on Dev.to Click below 👇

App's Overview

  • In this article, I am going to demonstrate how to create a simple country code picker in an android application through Kotlin.

  • Also, how to validate phone numbers for every country possible.

Table Of Content

Project Setup

  • Add the below line of code in build.gradle (project) under allprojects.
maven { url "https://jitpack.io" }

The code snippet will look like the one below :


allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
  • Dependency to be added : Add the dependency below in build.gradle (Module)
implementation 'com.github.joielechong:countrycodepicker:2.4.2'

The code snippet will look like the one below :

dependencies {

   ...
    other dependencies   
   ...

    /* Country Code Picker Dependency*/
    implementation 'com.github.joielechong:countrycodepicker:2.4.2'
}

App's UI

The app's UI is broken into two parts :

  1. Explanation of Country Code Picker (ccp)
  2. Complete .xml code file (activity_main.xml)

Explanation of ccp :

  • The below code snippet shows the implementation of the country code picker through the .xml file. The below code snippet is not customized, it is the naive form of ccp but it will work completely fine in the android application. We shall see the customization in the next section of the blog.
">

  

  • Customization of ccp in .xml file

    1. app:ccp_defaultNameCode="IN" - ccp_defaultNameCode attribute will set the the default country of the ccp spinner.

    2. app:ccp_defaultCode="91" - ccp_defaultCode will similarly set the default country code in the spinner.

    3. app:ccp_textColor="#1C2020" - change the color of ccp according to app's theme with ccp_textColor attritubute.

    4. app:ccp_countryPreference="IN,US,NZ" - ccp_countryPreference will set some of the preferred countries on the top, so that they will be easily accessed. seethe exaple below :

The customized code snippet :

">
  
  

There are many attributes present in order to customize the ccp, you can get to know about them all in it's documentation here.

Complete .xml file (activity_main.xml) :

">


   

    
    

        
     

        
     

    
    
    
    

   

App's Backend

  • Let's first link all the UI components with MainActivity.kt
val ccp:CountryCodePicker = findViewById(R.id.ccp)
val edtPhoneNumber: EditText = findViewById(R.id.edtPhone)
val btnOTP: Button = findViewById(R.id.btnSendOTP)

  • Tell ccp which edit text should be registered for phone numbers, use the below line of code
ccp.registerPhoneNumberTextView(edtPhoneNumber)
  • Now, let us create a function to check the validity of the phone number entered wrt the country code selected.

    ccp.isValid will check whether the phone number is correct or not.

The function looks like :

private fun checkValidity(ccp: CountryCodePicker): Boolean {

        return if(ccp.isValid) {
            Toast.makeText(this, "number " + ccp.fullNumber + " is valid.", Toast.LENGTH_SHORT).show()
            true
        } else {
            false
        }

    }

The function returns true if phone number entered is correct otherwise it will return false.

  • Finally, at last we will handle the button click and we are done to see the magic.
    btnOTP.setOnClickListener{
            
            if(checkValidity(ccp))
            {
                //Change with your own functionality
                Toast.makeText(this, "OTP Sent", Toast.LENGTH_SHORT).show()

            }else{
                Toast.makeText(this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show()
            }

        }

If the checkValidity() function returns true it will show a toast OTP Sent, you can change it with your function.


Writer's Support

  • If you find the article useful show some ❤️ by staring some of my repositories and following me on dev.to and github.
You might also like...
A TextView that automatically resizes text to fit perfectly within its bounds.
A TextView that automatically resizes text to fit perfectly within its bounds.

AutoFitTextView A TextView that automatically resizes text to fit perfectly within its bounds. Usage dependencies { compile 'me.grantland:autofitt

:page_facing_up: Android Text Full Jusiftication / Wrapping / Justify / Hyphenate - V2.0
:page_facing_up: Android Text Full Jusiftication / Wrapping / Justify / Hyphenate - V2.0

LIBRARY IS NO LONGER MAINTAINED If you want to adopt + maintain this library, please drop me a message - [email protected] Android Full Justific

A Custom TextView with trim text
A Custom TextView with trim text

ReadMoreTextView A Custom TextView with trim text Download To add the ReadMoreTextView library to your Android Studio project, simply add the followin

[DISCONTINUED] Rrich text editor for android platform. 安卓富文本编辑器,暂停维护

icarus-android Maybe the best rich text editor on android platform. Base on Simditor Features Alignment (left/center/right) Bold Blockquote Code Horiz

Android library contain custom realisation of EditText component for masking and formatting input text
Android library contain custom realisation of EditText component for masking and formatting input text

Masked-Edittext Masked-Edittext android library EditText widget wrapper add masking and formatting input text functionality. Install Maven dependency

An address-autocompleting text field for Android
An address-autocompleting text field for Android

android-PlacesAutocompleteTextView An AutocompleteTextView that interacts with the Google Maps Places API to provide location results and caches selec

AutosizeEditText for Android is an extension of native EditText that offer a smooth auto scale text size.
AutosizeEditText for Android is an extension of native EditText that offer a smooth auto scale text size.

AutoscaleEditText AutosizeEditText for Android is an extension of native EditText that offer a smooth auto scale text size. Latest Version How to use

Add text masking functionality to Android EditText. It will prevent user from inserting not allowed signs, and format input as well.
Add text masking functionality to Android EditText. It will prevent user from inserting not allowed signs, and format input as well.

MaskFormatter MaskFormatter adds mask functionality to your EditText. It will prevent user from inserting not allowed signs, and format input as well.

Path effect for text.
Path effect for text.

Screenshot Please waiting for loading the gif... ![] (/path4.gif) How to use Step 1: add denpendence compile('com.dk.view.patheffect:Library:0.1.1@aar

Comments
  • How to input hint text?

    How to input hint text?

    Hello How are you?

    https://user-images.githubusercontent.com/94693922/184633455-aa589d48-551a-4b38-8a5c-c1e00a05f5d0.png

    I want to set hint text like this. Please help me. Thanks.

    opened by edwinsong31 1
Owner
Siddharth Singh
I am a pre-final year student and a mobile app developer. I have worked in native android app development and I also worked with Flutter.
Siddharth Singh
Android form edit text is an extension of EditText that brings data validation facilities to the edittext.

Android Form EditText Android form edit text is an extension of EditText that brings data validation facilities to the edittext. Example App I built a

Andrea 1.5k Dec 14, 2022
Android Custom View for prevent the view behind on-screen keyboard when edit text is focused

Group Focusable Prevent the view behind on-screen keyboard when edit text is focused in Android UI Download Gradle implementation 'com.akexorcist:grou

Akexorcist 8 Jun 22, 2022
LocalizedEditText - Custom edit text that allow only one language

LocalizedEditText Custom edit text that allow only one language Supported languages : Arabic , English Default language : English Examples

Mostafa Gad 1 Nov 28, 2021
A editable text with a constant text/placeholder for Android.

ParkedTextView A EditText with a constant text in the end. How to use <com.goka.parkedtextview.ParkedTextView xmlns:app="http://schemas.android.co

goka 270 Nov 11, 2022
RTL marquee text view android right to left moving text - persian - farsi - arabic - urdo

RTL marquee text view android right to left moving text - persian - farsi - arabic - urdo

mehran elyasi 4 Feb 14, 2022
Float Label Edit Texts for Android

Floating Label Edit Text for Android For more info, see this blog post. Salient features Simple and clean library (open up the source to see for yours

null 428 Nov 25, 2022
Androids EditText that animates the typed text. EditText is extended to create AnimatedEditText and a PinEntryEditText.

AnimatedEditText for Android This repository contains AnimatedEditText and TextDrawable all of which extend the behaviour of EditText and implement fe

Ali Muzaffar 439 Nov 29, 2022
Simple way to create linked text, such as @username or #hashtag, in Android TextView and EditText

Simple Linkable Text Simple way to create link text, such as @username or #hashtag, in Android TextView and EditText Installation Gradle Add dependenc

Aditya Pradana Sugiarto 76 Nov 29, 2022
RichEditor for Android is a beautiful Rich Text WYSIWYG Editor for Android.

RichEditor for Android is a beautiful Rich Text WYSIWYG Editor for Android. Looking for iOS? Check out cjwirth/RichEditorView Supported Functions Bold

Daichi Furiya 6k Jan 2, 2023
Animation effects to text, not really textview

HTextView Animation effects with custom font support to TextView see iOS Effects see Flutter Effects Screenshot type gif Scale Evaporate Fall Line Typ

hanks 5.5k Jan 5, 2023