NAVIGATION - SEARCH

Android - How To Make Calls From Your App Programmatically

If you are developing any application which requires you to dial a phone number is very simple. Android provides inbuilt functionality to dial a particular number programatically. Here we are assuming that you have an EditText box for user to input phone number to dial and a Button to initiate the call.

To open the dialer we need to use Intent and the action to be performed here is ACTION_DIAL, and data to be provided is the phone number, which initiates call explicitly. Below is the code snippet that performs this operation.

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + strPH));

where strPH is the number to be dialed which got through EditText textbox. Call the startActivity to start the activity as shown below:

public class MainActivity extends Activity 
{
	EditText edittext;
	Button button;

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

		edittext = (EditText) findViewById(R.id.edittext);
		button = (Button) findViewById(R.id.button);
		button.setOnClickLictener(new OnClickListener()
		{
			@Override
			public void onClick(View v) 
			{
				String strPH = edittext.getText().toString().trim();
				Intent i = new Intent(android.content.Intent.ACTION_DIAL, Uri.parse("tel:+" + strPH));
				startActivity(i);
			}
		}
	}
}

This permission should be added to AndroidManifest.xml

<uses-permission android:name="android.permission.CALL_PHONE" />

Add comment