Android Phone Number Selector New Method

Deepak Pradhan
1 min readAug 10, 2020

--

Dependency

implementation 'com.google.android.gms:play-services-auth:18.1.0'
implementation 'com.google.android.gms:play-services-identity:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.4.0'
implementation 'com.google.android.gms:play-services-base:17.3.0'

1-Add these above dependency to the build build.grade module app

2-Call the Phone number selector where you want to add phone selector by adding bellow codes

private static final int CREDENTIAL_PICKER_REQUEST = 1;

HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();


PendingIntent intent = Credentials.getClient(getContext()).getHintPickerIntent(hintRequest);
try
{
startIntentSenderForResult(intent.getIntentSender(), CREDENTIAL_PICKER_REQUEST, null, 0, 0, 0,new Bundle());
}
catch (IntentSender.SendIntentException e)
{
e.printStackTrace();
}

3-Add these codes below the on create method of your activity or fragemt

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == RESULT_OK)
{
// Obtain the phone number from the result
Credential credentials = data.getParcelableExtra(Credential.EXTRA_KEY);
EditText.setText(credentials.getId().substring(3)); //get the selected phone number//Do what ever you want to do with your selected phone number here


}
else if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE)
{
// *** No phone numbers available ***
Toast.makeText(getContext(), "No phone numbers found", Toast.LENGTH_LONG).show();
}


}

Hint : It wont work with the emulator so try this with real physical device.

Previous google phone number selector api is depricated now so try this.

--

--

Deepak Pradhan
Deepak Pradhan

Responses (6)