Open a phone number in dialer

Open a phone number in dialer

We are working on one of our android app and in the same app we need to dlisplay User Profile information with mobile number. As per the requirement when user click on mobile number it will open phone dialer. In this article we are going to show you how to achieve the same. Below is the demo for final output.

As show in above video in the demo app we have a text view and a button. On both text view and button we have a on click listener which is calling same function “dialNumber()”. Once user click on either text view or button callNumber function called which contains code to dial number available in the text view.

Below is the MainActivity.xml file:

Code

<pre class="brush:xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;com.vlemonn.blog.demo.opendialer.MainActivity&quot;&gt;

    &lt;!--
        You can ignore or delete comments in your final coding.
        Comments are only for explanatory purpose.

    --&gt;

    &lt;!--
        We are using constraint layout if you want know
        more about constraint layout. Check below URL
        https://go.vlemonn.com/0lVeN2LJuf
    --&gt;


    &lt;!--
     Our Website: https://vLemonn.com
     Read this article: https://go.vlemonn.com/ec7HZ3Qvx0
     Watch Video: https://go.vlemonn.com/Y/ec7HZ3Qvx0
     --&gt;

    &lt;!--
        Text View contains Phone Number
        Text View: https://go.vlemonn.com/KbYFfDHqsl
    --&gt;
    &lt;TextView
        android:layout_width=&quot;368dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/tvSinglePhone&quot;
        android:text=&quot;+1-202-555-0194&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        android:layout_marginTop=&quot;72dp&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        android:layout_marginRight=&quot;8dp&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.0&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginEnd=&quot;8dp&quot; /&gt;

    &lt;!--
        Text View to display a text message for end user
        Click on number to make a call
     --&gt;
    &lt;TextView
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Click on number to make a call&quot;
        android:layout_marginTop=&quot;27dp&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/tvSinglePhone&quot;
        android:layout_marginRight=&quot;8dp&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.0&quot;
        android:id=&quot;@+id/textView&quot; /&gt;


    &lt;!--
        Button to make a call. Number is stored in tvSinglePhone
        Button: https://go.vlemonn.com/0yeOw53gZL
    --&gt;
    &lt;Button
        android:layout_width=&quot;368dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Make a Call&quot;
        android:layout_marginTop=&quot;32dp&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/textView&quot;
        android:layout_marginRight=&quot;8dp&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.0&quot;
        android:id=&quot;@+id/btnMakeCall&quot;
        /&gt;


&lt;/android.support.constraint.ConstraintLayout&gt;</pre>

<p>&nbsp;</p>

Below it the MainActivity.java file

Code

<pre class="brush:java;">
package com.vlemonn.blog.demo.opendialer;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    /*
    * Thanks for tunning in vlemonn.com
    * Here are some URLs
    * Our Website:              https://vlemonn.com
    * Our Youtube Channel:      https://go.vlemonn.com/YouTube
    * Support Us :              https://vlemonn.com/Donate
    * Ask Questions :           https://vlemonn.com/AskQuestion
    * Calendar :                https://vlemonn.com/Calendar
    * */

    /*
    * You can ignore or delete comments in your final coding.
    * Comments are only for explanatory purpose.
    */

    TextView tvSinglePhone;
    Button btnMakeCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* Text View */
        tvSinglePhone = (TextView) findViewById(R.id.tvSinglePhone);

        /* Button */
        btnMakeCall = (Button) findViewById(R.id.btnMakeCall);
        /*
        * Text View Click Listener on click it is going call function dialNumber().
        * You can read more about click listener here: https://go.vlemonn.com/99wVPgkuV9 or watch a video: https://go.vlemonn.com/Y/99wVPgkuV9
        * */
        tvSinglePhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialNumber();
            }
        });

        /*
        * Button Click Listener on click it is going call function dialNumber().
        * */
        btnMakeCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialNumber();
            }
        });



    }


    /*
    * dialNumber: This function is actually making call.
    * You can read more about function here: https://go.vlemonn.com/yaoMADYVLq or watch a video: https://go.vlemonn.com/Y/yaoMADYVLq
    * */
    private void dialNumber() {

        /*
            Intent
                An intent is an abstract description of an operation to be performed. It can be used
                with startActivity to launch an Activity, broadcastIntent to send it to any interested
                BroadcastReceiver components, and startService(Intent) or bindService(Intent
                , ServiceConnection, int) to communicate with a background Service.

                An Intent provides a facility for performing late runtime binding between the code
                in different applications. Its most significant use is in the launching of activities
                , where it can be thought of as the glue between activities. It is basically a
                passive data structure holding an abstract description of an action to be performed.

                You can read more about Intent here: https://go.vlemonn.com/u95G1CAnJf or watch a video: https://go.vlemonn.com/Y/u95G1CAnJf

        */
        /*
            String ACTION_DIAL
                String ACTION_DIAL
                Activity Action: Dial a number as specified by the data. This shows a UI with
                the number being dialed, allowing the user to explicitly initiate the call.

                Input: If nothing, an empty dialer is started; else getData() is URI of a
                phone number to be dialed or a tel: URI of an explicit phone number.

                Output: nothing.

                Constant Value: &quot;android.intent.action.DIAL&quot;
        */
        // Initialize an intent to open dialer app with specified phone number
        // It open the dialer app and allow user to call the number manually
        Intent intent = new Intent(Intent.ACTION_DIAL);
        // Send phone number to intent as data
        intent.setData(Uri.parse(&quot;tel:&quot; + tvSinglePhone.getText()));
        // Start the dialer app activity with the given number. In this we used +1-202-555-0194
        // It is a random US number.
        startActivity(intent);

    }
}</pre>

<p>&nbsp;</p>

Now let’s understand the code. Above code. In this demo app we used a TextView and a Button. Finally we attached setOnClickListener on text view and button. Both setOnClickListener calling function dialNumber(). Function dialNumber() contains actual code to dial a number or open phone dialer.

Below is the code for dialNumber() function.

Code

<pre class="brush:java;">
   /*
    * dialNumber: This function is actually making call.
    * */
    private void dialNumber() {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse(&quot;tel:&quot; + tvSinglePhone.getText()));
        startActivity(intent);
    }
</pre>

<p>&nbsp;</p>

In the fist line we are creating a new instance of Intent with Intent.ACTION_DIAL

Intent intent = new Intent(Intent.ACTION_DIAL);

Next we are setting data for our new intent. In the below line we are sending a Uri in the format tel:xxxxxxxxxx. Here xxxxxxxxxx is a phone number. And our phone number is stored in the TextView in MainActivity.xml file

intent.setData(Uri.parse("tel:" + tvSinglePhone.getText()));

Finally we are starting activity with our newly created Intent

startActivity(intent);

FileDownload
PhoneDialer.zipLogin to Download
0 Comments
Leave A Comment

Please login to post your valuable comments.

Join the newsletter

Get the latest vLemonn news first

share