How To Build A Phonecall App on Android - Hive Programmers
Greetings to my favorite science community online, StemSocial.
It's @skyehi and I'm very excited to be back to continue my Android programming tutorial series.
Original Image Source by fancycrave1 from Pixabay
We have successfully developed quite a lot of basic Android apps and each app development tutorial introduces us to new features and programming concepts.
The idea of this tutorial is to help improve your android app programming skills while having fun and developing cool apps.
For today's blog series we're going to be developing another simple yet very useful Android application. We're going to learn how to build an Android Phone call Application which the user can use to make a call.
Well this app may not be a full phonecall app where you have a dialer and other features like call history and bookmark. We'll be starting with a basic app that let's you call the number that you will insert in the codes while building the App.
It will be pretty simple and fun so let's get started guys.
PREREQUISITES
Alright guys, as usual before getting started with writing our codes, ensure that Android Studio is successfully installed on your PC. And also ensure that the Java Development Kit (JDK) is installed.
I took the Liberty of sharing the website link to downloading Android Studio.
Android Studio Download website.
Creating a New Project:
At this point I'll assume you have successfully installed Android studio and you're ready to create a new app project. To start, Open Android studio and select "Start a new Android Studio project."
Choose the right project template and for this app, you can select "Empty Activity" and click "Finish."
Layout Design:
Now that we're done creating a new app project, we'll begin creating the layout or frontend of our app. Like I mentioned earlier, it would be a really basic one so we'll just have a screen with a call button.
When the user presses the call button, it would call the number we will insert in the Java code or the logic of our app.
Open the res/layout/activity_main.xml
file create your call button.
Here's how your code should look like;
<?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=".MainActivity">
<Button
android:id="@+id/callButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Call" />
</RelativeLayout>
Creating the Backend or Logic of our App
It's time for us to create the logic of our app. The code we're about to write will activate "ACTION CALL" for your app to be able to make the call.
We'll write the code in src/main/java/com.example.yourappname/MainActivity.java
.
Here's how your code should look like!
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button callButton = findViewById(R.id.callButton);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Replace "phoneNumber" with the desired phone number
String phoneNumber = "tel:" + "123456789";
Intent dial = new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber));
startActivity(dial);
}
});
}
}
Please note that "123456789"
in the code snippet represents the number you want to call. By replacing it with a real number, you'll be able to make the phonecall.
Permission Code
We need to do this really important thing of adding the phone call access permission. Without this Permission code, our app will not be able to access the Phone Call Feature of the Phone.
This code will be written in AndroidManifest.xml
Here's the code to add to your Manifest file
<uses-permission android:name="android.permission.CALL_PHONE" />
Running Our App
Alright guys, we're done with the codes and it's time to run the app. You can connect your Android device or use an emulator to run the app.
When your app opens, Press the "Call" button, and the app should initiate a call to the specified number. In our code, it's going to call "123456789"
so you can replace that with a friend's number.
Congratulations! guys, you have successfully built a simple phone call app for Android using Android Studio.
Like I keep promising, we will be getting into more advanced apps after mastering the basic ones. So brace yourselves, it's going to get more fun in the future.
Thank you so much for taking the time to read today's blog. I hope you enjoyed it. If you had any trouble writing the codes or running the app, please do not hesitate to share it in the comments section below. I'll be more than happy to respond.
Have a lovely day and catch you next time on StemSocial. Goodbye 👨💻
You Can Follow Me @skyehi For More Like This And Others
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
Thanks for including @stemsocial as a beneficiary, which gives you stronger support.