Jetpack Compose Permissions using Accompanist library, ModalBottomSheet

Lukoh Nam
2 min readMay 14, 2023

--

Accompanist library provides Android runtime permissions support for Jetpack Compose.

First, you need to add this permission to the manifest file like this:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
repositories {
mavenCentral()
}
dependencies {
implementation "com.google.accompanist:accompanist-permissions:0.31.1-alpha"
}

Second, you’ve to check if the permissions are already granted, if it is you run the functions and if it is not you request it via the ModalBottomSheet.
You should hoist UI state to the lowest common ancestor between all the composables that read and write it. You should keep state closest to where it is consumed. From the state owner, expose to consumers immutable state and events to modify the state. This also makes your composables more reusable and easier to test. Please read state-hoisting if you’d like to learn it.

Third, you’ve to check if the permissions are already granted, if it is you run the functions and if it is not you request it via the ModalBottomSheet. Finally your composable looks like below. Please refer to onPermissionGranted and onPermissionDenied lamda expression below.

You need to create permissionState which will track the state of your permission and you’ve to check if the permissions are already granted, if it is you run the functions and if it is not you request it via the ModalBottomSheet.

After that, you are going to create Camera, Location Permission composable and pass it the permissions state as a parameter so it can handle permission. Inside this composable, you are using the accompanist’s PermissionRequired composable which takes in four parameters. Then you are going to add a ModalBottomSheet and a button for requesting these permissions in the Composable. In the button onClick callback you just launch a permission request with your permissionState. In the button onClick callback you just launch the permissions request with your permissionState.

After that, you’ve to request the permissions to the user. After that, you’ve to request the permissions to the user.

Thank you so much for reading and I hope this article was helpful. If you have something to say to me, please leave a comment, and if this article was helpful, please share and clicks 👏 👏 👏 👏 👏.

Please visit the Phogal Github & Medium link below if you’d like to dive deep into more advanced…

Phogal :

Github : https://github.com/Lukoh/Phogal

Medium : https://medium.com/@lukohnam/profiler-a54f4f0f48f

Let me know or email me if you’re interested in my experience and techs. Please refer to my LinkedIn link below:

LinkedIn : https://www.linkedin.com/in/lukoh-nam-68207941/

Email : lukoh.nam@gmail.com

Android

Android App Development

--

--