1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Assessing the Features of Android Devices with Kotlin and Java

Making a Call in Android, Kotlin, and Java

Start course
Overview
Difficulty
Intermediate
Duration
1h 19m
Students
2
Description

In this course, we'll cover how to access the features of Android devices. You'll learn how to send an SMS, send an email, make a call, and convert speech to text on Android.

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.

Prerequisites

To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.

 

Transcript

And a way we go again. Hello to everybody? So, now we're going to learn how to make calls. What? It is a phone after all. Okay, so this is the third subject of accessing the features of the Android device. So, it's through the new Android application we'll be able to reach the call section of the Android phone and call whatever number we want. Old school style, right? Okay, so let's just shuffle on over to Android Studio and continue. So, I created a new project in Android Studio just for this lesson. Now, before of course we're going to the details, I do want to show you what it looks like and when we get there.

So, what you're looking at is an  EditText and a button. Yeah, so when the user types the number that they want to call, presses the 'Call' button. Well, you learned this before, right? You got to ask permission. And if the user allows us to have to make a call, then the number will be automatically dialed. So, what happens when we close the call?  It returns to the application? All right, so here we get the number to be called from the user. We could also if we want to, just construct a fixed number that we set like a speed dial type thing.

For example, if let's say it's a mobile banking application when the user wants call customer service, what do they do? They can call customer services by pressing an icon or a button in the application, right? Same idea here. The user can call customer service directly just by pressing on the number without, well, having to remember it, leaving the application or coming back to find it, that kind of thing. So, naturally it's just a template. This method can be used in many other areas as well. We could also make the dialing screen pop up when the user presses a button. So, instead of dissecting this any further or talking about something else, why don't we just get started and construct the application? You ready to develop? Of course, let's do it.

Android Studio and we're looking at an EditText and a button in the design area. First, I do want to add a phone EditText. So, the width of the EditText can be 300dp. The height of the EditText can be 60. Also the text size can be 20sp, text color will be black. And last up, I do want to add a hint to this component. So, the hint can be enter a phone number. All right, so now I can add a button. Width of the button can be 100dp, height can be 60dp. I'll change the text of the button. And, I'll just say call, okay?

Now, let's increase the text size a little bit, text size can be 20sp. Okay, so since the main layout is constraint layout, I'm going to determine the constraint values of these components. So, I'm going to select both components. Right click with the mouse and select the 'Center Horizontally' option. And, what about these vertical constraints? The top constraint value of the EditText can be 100dp. Top constraint value, let's make it 50. And last up, how about an id to give these components? The id of the EditText can be editTextPhone. Id of the button, about buttonCall.

Okay, that's the design of the app. Now, let's continue on the column side. So, let's define the components first. Of course, you can use the Android view binding features so that way you don't have to use the findViewById function. I don't know how much you like it. I actually just, out of habit I think, use findViewById, just because that's the way I'm used to. But I also use Android view binding, especially when, this is the thing, especially when there are too many components in the design area, all right? So, let's just go to the onCreate function and write, call = findViewById(R.id.buttonCall). And phone = findViewById(R.id.editTextPhone).

So, now I'll add a ClickListener, and I'll write call.setOnClickListener{}. So, what am I going to put inside the curly braces? Remember, I'm going to get the phone number entered by the user in the EditText, pass it to a variable, but I will create this variable above in the global area because I'm going to be using this variable in different places in the application. So, I need to be able to access it from anywhere in this class. So, I'll write var userNumber : String = "". Now I can continue in the ClickListener. So, I'll write userNumber = phone.text.toString(). All right, so that's all, nothing new, right? You could have done that in your sleep. So, let's just go ahead and do some of these actions to be able to make this call and then I'll lay something on you. So, in order to make a call, we need to first ask the user permission. And of course we'll perform the user permission again;

two steps. First, we have to specify the permission that we will receive in the manifest file. Secondly, we have to write some sort of code here. So, we'll open manifest file first, get that necessary permission. Permission to make a call is CALL _PHONE. Okay, we'll go back to the column side, and here is where we will create a call function and perform the operations in this function. So, the name of the function can be startCall(). Also, this function will take a parameter. Of course, it's the user number. Now to make a phone call, we will use intent again. So, I'm creating a new intent here, I'll write val intent = Intent(Intent.ACTION_CALL). And on the bottom line, I'll write intent.data, as the method. To the right of the equal sign,  I write Uri.parse(), to indicate to the system that the data we received from the user is a phone number in parentheses I write "tel", which is the abbreviation for telephone, obviously in small letters and put a colon. So, this what you see my friends is a standard expression. If you don't use a colon here or capitalize the letter T,

well, you're not going to be able to perform the call. So, you got to pay attention to this, right? So, now we're going to write the phone number entered by the user, all right? So, I'll put $, then I write user number because we got the number that will be called from the user. Okay, so I'll need to run this intent. So, I'm writing startActivity(Intent). And normally the call should be done like this, right? Just this way, but these codes actually do not work alone in newer versions of Android operating system. In the top version, such as Oreo and Pie, we're going to need to add some other code to this, but it will work this way in older version. So, we'll start here.

All right, so in these newer versions of Android, like API 29, 30, and 31, the call process is a process that requires higher security. So, if you wind up just getting the permission from the manifest file, it's not going to cut it or it's not going to call it, right? So, here obviously a second permission is required from the user. So, for this, I'll create an if condition here,  I'll write if (ContextCompact.checkSelfPermission()). So, this method takes two parameters. First parameter will be context. We can of course use our favorite this keyword as context. The second parameter needs to be specified for permission from the manifest file.

So, I write Manifest.permission.CALL_PHONE. So, in other words, the permission from the manifest file is specified first. Then I'll add the not equal sign. So, then this if condition will work if the user doesn't allow it, go to the bottom line, because it doesn't fit on the screen, after it is not equal. I'll write, PackageManager.PERMISSION_GRANTED. So, I specify the permission to be obtained from the user after it is not equal. So, this section is the control clause of the if condition. So, now let's go ahead and write the inner part of the if condition. So, I called the method ActivityCompat.requestPermissions. So, in other words, I ask the user for permission. And this method will take three parameters. The first parameter will be context, and we can use this as our keyword for context. And the second parameter is an array. So, I'll create this array by typing arrayOf(). Now, the elements of this array will be permissions from the user.

So, I need to specify how many permissions I'll get from the user here. I'll just take the one permission for now, and it'll be the call permission. But if had to get other permissions from the user, I would be able to add those permissions to this array. So, this is like reaching a telephone directory, contact list, that kind of thing. We're going to specify the call permission. I'll write Manifest.permission.CALL_PHONE. So, that was the second parameter. Third parameter is request code. I'll write 100 in the request code. And if you remember, I wrote one in the request code while doing the S.M.S functions. So, I'll write 100 for the call. You can use any energy value that you want as a request code. We'll just use this code to check the result of the permission request.

So, when a user installs and uses the application for the first time, the codes here will run, the user will be asked for permission since they haven't given permission to the application yet. But we want the application  to run when the user allows it. So, we will need to track whether or not the user has given permission, and that's why I'm calling the onRequestPermissionResult() method outside of the onCreate function. And here, too, we will check whether the permission is granted using the if statement. So, if the request code equals 100 and the size of the grant result is greater than zero, and the first element of the grant result array is the PackageManager.PERMISSION_GRANTED, then the user has given permission. In this case, copy and paste the codes that we wrote to call. Now, of course, the second and the following times that the user uses the application, we won't bother asking the user for permission because user already gave the permission the first time around. So, we don't have to repeatedly ask for permissions from the user.

So, in this case too, a direct call- it's got to be made without opening the permission window. So, that means I need to create an else block here. So, I'm cutting and pasting the code that we wrote to call. So, our call method is ready. Now, let's call this method inside the click listener of the call button. I'll write Start call here. It's parameter will be user number. So, now we can run the application. Let's have a look. So, it opens up. I'll write in a phone number. Now, let's click on the call button. So, our application asks the user for permission. So, those codes have worked. So, I'll just click allow. What do you know? It starts at call. So, those codes here have worked. So, I'll just end the call, and when I finished calling, it returns to the application, and I'll exit the app. So, I want to open up the app once again. Typing a number and click on the 'call' button, and there you go.

See, it doesn't ask for permission anymore, and it just performs a call right away. So, all of those codes are working here. So, I think that is clear enough for that. So, right now, let's do the necessary actions to open the number dialing screen. It's a whole other kettle of fish. But before you get ahead of yourself, it's very easy. Because we can just change the action here. So, write ACTION_DIAL instead of ACTION_CALL, and what do you know? That's it. So, let's just run the application once more. Write in the phone number, press the call button. And as you can see, this time, it didn't just call the number directly. The dialing window popped up, and now, of course, you can use a different number from here, or you can call the number that you wrote. You can change the number, whatever you want. So, that's it for this video. We've learned how to do calls made through the android application. I want to see in the next video there's more to do.

 

About the Author
Students
86
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics