Open In App
Related Articles

How to Use Phone Selector API in Android?

Improve Article
Save Article
Like Article

Phone Selector API is used to detect phone numbers being used in the phone. Using this you can avoid manual input of Phone Number by users and prompt them to choose the desired number. A sample image is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language

Step 2: Add dependency to the build.gradle file and click “sync now”

  implementation “”

Step 3: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. For simplicity, we are using just a TextView to show the number after selection. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />
        app:layout_constraintTop_toBottomOf="@id/tv1" />

Step 4: Inside MainActivity.kt write the following code

  • To retrieve the Phone Number hints, first, configure the hint selector dialog by creating a HintRequest object. Then, pass the HintRequest object to credentialsClient.getHintPickerIntent() to get an intent to prompt the user to choose a phone number. Finally, start the intent with startIntentSenderForResult().
  • onActivityResult() method will help to get the number user has selected and then you can write the next logic to continue with your app.


import android.content.Intent
import android.content.IntentSender
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    lateinit var open_btn: Button
    lateinit var tv1: TextView
    companion object {
    override fun onCreate(savedInstanceState: Bundle?) {
        open_btn = findViewById(
        tv1 = findViewById(
        // set on click listener to button 
        // to open the phone selector dialog
        open_btn.setOnClickListener {
    private fun phoneSelection() {
        // To retrieve the Phone Number hints, first, configure
        // the hint selector dialog by creating a HintRequest object.
        val hintRequest = HintRequest.Builder()
        val options = CredentialsOptions.Builder()
        // Then, pass the HintRequest object to 
        // credentialsClient.getHintPickerIntent()
        // to get an intent to prompt the user to 
        // choose a phone number.
        val credentialsClient = Credentials.getClient(applicationContext, options)
        val intent = credentialsClient.getHintPickerIntent(hintRequest)
        try {
                CREDENTIAL_PICKER_REQUEST, null, 0, 0, 0, Bundle()
        } catch (e: IntentSender.SendIntentException) {
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == RESULT_OK) {
            // get data from the dialog which is of type Credential
            val credential: Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY)
            // set the received data t the text view
            credential?.apply {
                tv1.text =
        } else if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE) {
            Toast.makeText(this, "No phone numbers found", Toast.LENGTH_LONG).show();


Github Link:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 15 Jan, 2021
Like Article
Save Article
Similar Reads