Sat. Nov 23rd, 2024

Here’s how you can create a simple Android application with an XML layout file for a button that makes a phone call, and a MainActivity written in Kotlin.

XML Layout (activity_main.xml)

Place this code in the res/layout/activity_main.xml file. It creates a button that will trigger the phone call.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp">

    <Button
        android:id="@+id/callButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Make a Call"
        android:padding="16dp"
        android:backgroundTint="@color/purple_500"
        android:textColor="@android:color/white" />

</LinearLayout>

MainActivity (MainActivity.kt)

This is the main activity code that handles the phone call and permission request. Place this code in MainActivity.kt.

package com.androindain.call

import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

private val CALL_PERMISSION_REQUEST_CODE = 1

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val callButton = findViewById<Button>(R.id.callButton)
callButton.setOnClickListener {
makePhoneCall("1234567890") // Replace with the number you want to call
}
}

private fun makePhoneCall(phoneNumber: String) {
// Check if the CALL_PHONE permission is granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED) {
val intent = Intent(Intent.ACTION_CALL)
intent.data = Uri.parse("tel:$phoneNumber")
startActivity(intent)
} else {
// Request the CALL_PHONE permission
ActivityCompat.requestPermissions(
this, arrayOf(Manifest.permission.CALL_PHONE), CALL_PERMISSION_REQUEST_CODE
)
}
}

// Handle permission request result
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<out String>, grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == CALL_PERMISSION_REQUEST_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, make the call
makePhoneCall("1234567890")
}
}
}
}

Android Manifest (AndroidManifest.xml)

Don’t forget to add the necessary permission in your AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yourappname">

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.YourAppName">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

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

</manifest>

Explanation

  • Button (callButton): When the button is clicked, it calls the makePhoneCall function.
  • makePhoneCall Function: This function checks if the app has the CALL_PHONE permission. If granted, it starts the call. If not, it requests the permission.
  • onRequestPermissionsResult: This handles the permission request result. If the user grants permission, it makes the call.

This completes the setup for making a phone call using a button click in Android with Kotlin.

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *