Apps need permission to access sensitive features like camera or location. This flow helps ask users for permission politely.
0
0
Permission request flow in Android Kotlin
Introduction
When your app needs to use the camera to take pictures.
When your app wants to access the user's location for maps.
When your app needs to read contacts or files.
When you want to respect user privacy and follow Android rules.
Syntax
Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.PERMISSION_NAME) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.PERMISSION_NAME), REQUEST_CODE) } else { // Permission already granted }
Replace PERMISSION_NAME with the actual permission like CAMERA or ACCESS_FINE_LOCATION.
REQUEST_CODE is a number you choose to identify this permission request.
Examples
This checks if camera permission is granted. If not, it asks the user.
Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 100) } else { // Camera permission already granted }
This handles the user's answer to the permission request.
Android Kotlin
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 100) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
// Permission granted, proceed
} else {
// Permission denied, show message
}
}
}Sample App
This app checks if the camera permission is granted when it starts. If not, it asks the user. It shows a small message depending on the user's choice.
Android Kotlin
import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat class MainActivity : AppCompatActivity() { private val CAMERA_REQUEST_CODE = 100 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) checkCameraPermission() } private fun checkCameraPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE) } else { Toast.makeText(this, "Camera permission already granted", Toast.LENGTH_SHORT).show() } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == CAMERA_REQUEST_CODE) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show() } } } }
OutputSuccess
Important Notes
Always explain why you need the permission before asking.
Users can deny permission permanently; handle that case gracefully.
Test on devices with different Android versions because permission behavior can vary.
Summary
Check if permission is already granted before asking.
Request permission using ActivityCompat.requestPermissions.
Handle user response in onRequestPermissionsResult.