Build Calling App in Android

 Hello Guys,

      In this tutorial , we will make you phone call from android app.

      you can do so with help of Intent with action as ACTION_CALL. Basically Intent is a simple message object that is used to communicate between android components. Here it is use to make phone call. This Application contain one activity with Editext to write phone number and Button to call that phone number. Calling App Main Screen looking like below picture:

Step 1: Add Permission in Android-Manifest.xml

  • Here you need to add permission in Android-Manifest.xml file. Here CALL_PHONE permission is needed to add in Android-Manifest.xml file for making phone call.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calling_app">

<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/Theme.Calling_App">
<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>

Step 2: activity_main.xml 
  • activity_main.xml file contain RelativeLayout which contain EditText and Button. EditText is used to write phone number. And Button is used to make phone call.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="250dp"
android:inputType="phone"
android:hint="Enter Phone number"
android:ems="10" />


<Button
android:id="@+id/button1"
android:layout_below="@+id/editText1"
android:background="@drawable/call_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_centerHorizontal="true"
android:text="Call" />

</RelativeLayout>
Here you can change button shape using call_button.xml file. Create call_button.xml file under drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/teal_200"/>
<corners android:radius="120dp"/>

</shape>
 
Step 3: MainActivity.java
  • In MainActivity Intent object is created to redirect activity call manager and action attribute of intent is set as ACTION_CALL.
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText phone_number;
Button call_btn;

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


phone_number = (EditText) findViewById(R.id.editText1);
call_btn = (Button) findViewById(R.id.button1);


call_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

CallTime();
}
});

}

private void CallTime() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1);
}
else
{
String number = phone_number.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
startActivity(callIntent);

}

}

@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {

if (requestCode == 1) {

if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
CallTime();
} else {
Toast.makeText(this, "Permission Denied. Try Again!",
Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Output:


If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏










Post a Comment

0 Comments