How to get phone number in android?

This example demonstrate about How to get phone number in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
< xmlns:android = ""
   xmlns:app = ""
   xmlns:tools = ""
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">

      android:id = "@+id/phoneNumber"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "phone number"
      app:layout_constraintBottom_toBottomOf = "parent"
      app:layout_constraintLeft_toLeftOf = "parent"
      app:layout_constraintRight_toRightOf = "parent"
      app:layout_constraintTop_toTopOf = "parent" />

In the above code, we have taken text view. When user open application, Phone number going to append on textview.

Step 3 − Add the following code to src/

package com.example.andy.myapplication;

import android.Manifest;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

import static android.Manifest.permission.READ_PHONE_NUMBERS;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.READ_SMS;

public class MainActivity extends AppCompatActivity {
   TextView textView;
   @RequiresApi(api = Build.VERSION_CODES.M)
   protected void onCreate(Bundle savedInstanceState) {
      textView = findViewById(;
      if (ActivityCompat.checkSelfPermission(this, READ_SMS) == PackageManager.PERMISSION_GRANTED &&
         ActivityCompat.checkSelfPermission(this, READ_PHONE_NUMBERS) ==
         PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
         READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            TelephonyManager tMgr = (TelephonyManager)   this.getSystemService(Context.TELEPHONY_SERVICE);
            String mPhoneNumber = tMgr.getLine1Number();
      } else {
   private void requestPermission() {
      if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) {
         requestPermissions(new String[]{READ_SMS, READ_PHONE_NUMBERS, READ_PHONE_STATE}, 100);
   public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
      switch (requestCode) {
         case 100:
            TelephonyManager tMgr = (TelephonyManager)  this.getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) !=  
               PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
               Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED  &&
               ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) !=      PackageManager.PERMISSION_GRANTED) {
            String mPhoneNumber = tMgr.getLine1Number();

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

When user enter into application, it will ask run time permission. Allow all permission, it will show phone number as shown above.

Click here to download the project code.

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started