Home »
Android
Android code to make call from your own application
Android code to make call from your own application: In this tutorial, we will learn how to develop an Android app to make call. This tutorial contains all source codes files related to this android project (code for calling from android mobile).
Last updated : June 06, 2023
Android code to make call from your own application
This application can be used to call from an android mobile, since we are learning here to write code in Android, so we are not attaching the complete project here.
Note: You have to create a project and copy the source code and paste it in the related files.
Source code files to make call from your own application
The files for "calling from an Android phone" are:
- MainActivity.java
- AndroidManifest.xml
- activity_main.xml
- Output files
1) MainActivity.java
package com.includehelp.autodial;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
public static final int MULTIPLE_PERMISSIONS = 101;
EditText number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number =(EditText)findViewById(R.id.mobnum);
//asl for runtime permission
checkPermissions();
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MULTIPLE_PERMISSIONS :
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"Permission Granted",Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onResume() {
super.onResume();
}
//Call on Button click
public void dialCall(View view){
String num = number.getText().toString();
if(num!=null && !num.equals("")){
String noToDial = "tel:"+num;
Uri number = Uri.parse(noToDial);
Intent callIntent = new Intent(Intent.ACTION_CALL, number);
if(checkPermissions()){
startActivity(callIntent);
}
}
else{
Toast.makeText(this,"Please enter number",Toast.LENGTH_SHORT).show();
}
}
// For runtime Permission
private boolean checkPermissions() {
int result;
String[] permissions= new String[]{
Manifest.permission.CALL_PHONE};
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p:permissions) {
result = ContextCompat.checkSelfPermission(this,p);
if (result != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG,"Ask Permissions : "+p);
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),MULTIPLE_PERMISSIONS );
return false;
}
return true;
}
}
2) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.includehelp.autodial">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.includehelp.autodial.MainActivity">
<EditText
android:id="@+id/mobnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Enter Number for call"
android:inputType="number"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/mobnum"
android:layout_marginTop="20dp"
android:text="Make Call"
android:onClick="dialCall"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Output files...
1) Main screen
2) Enter Number and DIAL and click on "Make Call"
3) Call dialling screen