Android Phone number Calling Function in an Activity

11,730

Maybe something like:

Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:" + findViewByid(R.id.textView4).getText());
startActivity(call);
Share:
11,730
Avadhani Y
Author by

Avadhani Y

My Career Profile

Updated on June 04, 2022

Comments

  • Avadhani Y
    Avadhani Y almost 2 years

    I am developing Android Application having ContactUs Page having Phone Number. I had given the Phone number in xml file as below:

    <TextView 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:text="1-869-270-9099" 
    android:textSize="11sp"
    android:textColor="#104082"
    android:textStyle="normal"
    android:layout_width="wrap_content" 
    android:id="@+id/textView4" 
    android:layout_x="72dp" 
    android:layout_y="160dp"/>
    

    I had created the AlertDialog and when the phone number is clicked the alert dialog will be shown programatically as below:

       @Override
       protected Dialog onCreateDialog(int id) {
        switch (id) {
        case (R.id.textView4):
    
            Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Do you want to Call?");
            builder.setCancelable(false);
            builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                   //Do Calling a Number
    
    
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        }
        return super.onCreateDialog(id);
    }
           public void onClick(View v) {
    
        switch(v.getId()){
        case (R.id.textView4):
    
            showDialog(R.id.textView4);
        break;
        }
    
    }
    

    Here my issue is to implement the "Calling a Phone Number" Function in the onClick "PositiveButton" method. Please help me how to get a DialPad with the number present in the xml file with the SampleCode/Links.

  • Iiro Krankka
    Iiro Krankka over 12 years
    You also might want to have the phone number in /res/values/strings.xml like this: <string name="phone_num">1-869-270-9099</string> and on your TextView <TextView android:text="@string/phone_num" .../>. Then just call getResources().getString(R.string.phone_num) whenever you need the number in your code. This way it is easier to maintain your application and changing the phone number is easier, since you only have to change it from strings.xml and it's updated automatically everywhere where you need it in your app.