Android Country Code Picker | Kotlin | Android | CCP (Country Code Picker)
2 min readJan 7, 2023
Hello, In This tutorial will learn how to used country code picker(ccp) in android studio using Kotlin.
STEP 1 : Create new Android Studio Project.
STEP 2 : After Creating new Project Setup build.gradle and sync project
dependencies {
implementation 'com.hbb20:ccp:2.4.2'
}
STEP 3: Add CountryCodePicker to your activity_main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="5dp"
android:id="@+id/llNumber">
<com.hbb20.CountryCodePicker
android:id="@+id/ccp"
app:ccp_showFullName="false"
app:ccp_showPhoneCode="true"
app:ccp_showNameCode="false"
app:ccp_showFlag="true"
app:ccp_autoFormatNumber="true"
app:ccp_showArrow="true"
app:ccp_textSize="16sp"
app:ccp_arrowSize="16sp"
app:ccpDialog_keyboardAutoPopup="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:hint="Phone number"
android:textColor="@color/black"
android:inputType="phone"
android:id="@+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:text="Check Full Number"
android:layout_margin="10dp"
app:layout_constraintTop_toBottomOf="@id/llNumber"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
STEP 4: Add CountryCodePicker to your MainActivity.kt Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.hbb20.CountryCodePicker
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// declaring ccp and editText
val ccp: CountryCodePicker = findViewById(R.id.ccp)
val etPhoneNumber: EditText = findViewById(R.id.etPhoneNumber)
// Connecting editTextPhoneNumber with country code picker
ccp.registerCarrierNumberEditText(etPhoneNumber)
val btnCheck: Button = findViewById(R.id.btnCheck)
btnCheck.setOnClickListener {
Toast.makeText(this, "Number: "+ccp.fullNumber, Toast.LENGTH_LONG).show()
}
}
}
// All code is done let's run the app
STEP 5 : And finally run the App and See the output
More info about ccp Library: https://github.com/hbb20/CountryCodeP...
Show More Tutorials And Feel free to SUBSCRIBE to my YOUTUBE channel Thank You :-)