Implicit Intent-Open Url & Call Phone Number - Android Studio - Kotlin

In this example, we will open https url & call phone number with permission code. See the following example:

Step.1

AndroidManifest.xml

Add the following permission  in the AndroidManifest.xml file.

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

Step.2

MainActivity.kt

Add the following code in the MainActivity.kt class.

package com.study.kotlinkatta

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

class MainActivity : AppCompatActivity() {

lateinit var btn_click_to_open_url: Button
lateinit var btn_click_to_call: Button

private val TAG = "Permission"
private val CALL_REQUEST_CODE = 101

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

btn_click_to_open_url =findViewById(R.id.btn_click_to_open_url)
btn_click_to_open_url.setOnClickListener {

val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.google.com")
startActivity(intent)
}

btn_click_to_call =findViewById(R.id.btn_click_to_call)
btn_click_to_call.setOnClickListener {

setupPermissions()
}
}

private fun setupPermissions() {
val permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE)

if (permission != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission to call denied")
makeRequest()
} else {
val intent_call =
Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Enter Phone Number"))
startActivity(intent_call)
}
}

private fun makeRequest() {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CALL_PHONE),
CALL_REQUEST_CODE)
}
}

Step.3

activity_main.xml

Add the following code in the activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_click_to_open_url"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Click To Open Url"/>

<Button
android:id="@+id/btn_click_to_call"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Click To Call Phone No"/>

</LinearLayout>
Output:




Comments