9

How can I make call by pressing button? I get my number as a string from EditText. Here is my sample code:

String phone = editPhone.getText().toString();
btnPhone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                call();
            }
        });
public void call() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
         Log.e("myphone dialer", "Call failed", e);
    }
}

I added all permissions to manifest file.

but I am getting NullPointerexception

user1383729
  • 111
  • 1
  • 1
  • 4

7 Answers7

27

This simple approach should work for you.

Ex.

public class CallActivity extends Activity{
   String phone = "";

   onCreate()
   {
        btnPhone.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                phone = editPhone.getText().toString(); 
                call(); 
            } 
        });    
   }

   public void call() {   
            Intent callIntent = new Intent(Intent.ACTION_CALL);          
            callIntent.setData(Uri.parse("tel:"+phone));          
            startActivity(callIntent);  
   }
}

You might be using String variable phone out of scope.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
11

I think you missed the "tel:" part in the URI.

Replace the following..

Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);

with

Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
       startActivity(callIntent);

or

Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+phone));
            startActivity(callIntent);
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
5

see below code it may help you.

for call

EditText num = (EditText)findViewById(R.id.number_edit);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);

for open dialer

Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"));
startActivity(dial);
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • call end worked in my app. see if i use above code then phone dialer open and there is red button for end call. check it again. – Dhaval Parmar May 09 '12 at 06:30
3
String PhoneNo="+923341234567"
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + PhoneNo));
startActivity(intent);

and add a permission in manifest

<uses-permission android:name="android.permission.CALL_PHONE" />
Drunix
  • 3,313
  • 8
  • 28
  • 50
M.Saleh
  • 39
  • 5
2
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(phone));
startActivity(callIntent);

above is the only method you use for calling a phone number in android, when you click on the call button a 'dilerpad' activty is started with perpopulated phone number, and the call will go if you press the call button on the dialer pad.

Shams Ud Din
  • 99
  • 2
  • 8
0
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" +"93");
intent.setData(Uri.parse(uri));
startActivity(intent);
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

Try this

EditText num = (EditText)findViewById(R.id.phone_number);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(intent);
Mwongera808
  • 880
  • 11
  • 16