Lädt...


🔧 Build a Flashlight in Jetpack Compose


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Today we will discuss how to build a flashlight in Jetpack Compose.

Step-by-step Guide

  1. Setting up dependency
  2. Setting up Manifest
  3. Code Explanation
  4. Code Implementation

Setting up dependencies
We will be utilising Google's new API for permission requests in our app as it provides a more elegant way of requesting permission in our apps. Since it is a work in progress, there are some known limitations like not being able to check if the user is requesting the app for the first time etc. For more info, check this out

Now, in your build.gradle.kts or build.gradle file add the dependency

 // Dependency to request hardware permission
 implementation ("com.google.accompanist:accompanist-permissions:0.32.0")

Setting up Manifest
In your AndroidManifest.xml file, add the code below inside your application tag

 <uses-feature
   android:name="android.hardware.camera"
   android:required="false" />
 <uses-permission android:name="android.permission.CAMERA" />

Code Explanation

fun toggleFlashLight(cameraManager: CameraManager, cameraId: String, isFlashOn: Boolean) {
    try {
        cameraManager.setTorchMode(cameraId, isFlashOn)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun handlePermissionDenied(context: Context) {
    Toast.makeText(context, "Please grant camera permission", Toast.LENGTH_LONG).show()
}

fun handlePermissionRationale(context: Context) {
    Toast.makeText(context, "Camera permission is needed", Toast.LENGTH_LONG).show()
}
@OptIn(ExperimentalPermissionsApi::class)
fun handleLogic(
    cameraPermissionState: PermissionState,
    context: Context,
    cameraManager: CameraManager,
    cameraId: String,
    isFlashOn: Boolean,
    updateState: (Boolean) -> Unit,
) {
    if (cameraPermissionState.status.isGranted) {
        val newState = !isFlashOn
        updateState(newState)
        toggleFlashLight(cameraManager, cameraId, newState)
    } else if (cameraPermissionState.status.shouldShowRationale) {
        handlePermissionRationale(context)
        cameraPermissionState.launchPermissionRequest()
    } else {
        handlePermissionDenied(context)
        cameraPermissionState.launchPermissionRequest()
    }
}

Code Implementation

import android.Manifest
import android.content.Context
import android.hardware.camera2.CameraManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.kotlin_articles.ui.theme.KotlinarticlesTheme
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionState
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import com.google.accompanist.permissions.shouldShowRationale


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            KotlinarticlesTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun Greeting(modifier: Modifier = Modifier) {
    val context: Context = LocalContext.current
    val cameraPermissionState = rememberPermissionState(permission = Manifest.permission.CAMERA)
    var isFlashOn: Boolean by rememberSaveable {
        mutableStateOf(false)
    }
    val cameraManager: CameraManager = remember {
        context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    }
    val cameraId: String = rememberSaveable {
        try {
            cameraManager.cameraIdList[0]
        } catch (e: Exception) {
            "0"
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        Button(
            modifier = Modifier.height(50.dp),
            onClick = {
                handleLogic(
                    cameraPermissionState,
                    context,
                    cameraManager,
                    cameraId,
                    isFlashOn
                ) { isFlashOn = it }
            },
        ) {
            Text(
                text = "Turn flashlight ${if (isFlashOn) "Off" else "On"} !",
                fontSize = 16.sp,
                textAlign = TextAlign.Center
            )
        }
    }
}

fun toggleFlashLight(cameraManager: CameraManager, cameraId: String, isFlashOn: Boolean) {
    try {
        cameraManager.setTorchMode(cameraId, isFlashOn)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun handlePermissionDenied(context: Context) {
    Toast.makeText(context, "Please grant camera permission", Toast.LENGTH_LONG).show()
}

fun handlePermissionRationale(context: Context) {
    Toast.makeText(context, "Camera permission is needed", Toast.LENGTH_LONG).show()
}

@OptIn(ExperimentalPermissionsApi::class)
fun handleLogic(
    cameraPermissionState: PermissionState,
    context: Context,
    cameraManager: CameraManager,
    cameraId: String,
    isFlashOn: Boolean,
    updateState: (Boolean) -> Unit,
) {
    if (cameraPermissionState.status.isGranted) {
        val newState = !isFlashOn
        updateState(newState)
        toggleFlashLight(cameraManager, cameraId, newState)
    } else if (cameraPermissionState.status.shouldShowRationale) {
        handlePermissionRationale(context)
        cameraPermissionState.launchPermissionRequest()
    } else {
        handlePermissionDenied(context)
        cameraPermissionState.launchPermissionRequest()
    }
}

Happy coding!!!

You can check out the repo

...

🔧 Build a Flashlight in Jetpack Compose


📈 51.22 Punkte
🔧 Programmierung

🔧 What’s New with Android Jetpack and Jetpack Compose


📈 39.42 Punkte
🔧 Programmierung

🔧 Jetpack Compose Mastery Series Finale: 38 Key Issues in Compose UI


📈 37.49 Punkte
🔧 Programmierung

🔧 Jetpack Compose Mastery Part 2: Advanced Tools and Resources for Mastering Compose UI


📈 37.49 Punkte
🔧 Programmierung

🔧 Jetpack Compose Mastery Part 1: A Comprehensive Guide to Building Your First Compose Application


📈 37.49 Punkte
🔧 Programmierung

🔧 Writing cleaner Jetpack Compose code with the Twitter Compose Ruleset


📈 37.49 Punkte
🔧 Programmierung

🎥 Learn Jetpack Compose at Compose Camp


📈 37.49 Punkte
🎥 Video | Youtube

📰 Learn Jetpack Compose at a Compose Camp near you!


📈 37.49 Punkte
🤖 Android Tipps

🔧 Developers for adidas CONFIRMED build features 30% faster using Jetpack Compose


📈 30.36 Punkte
🔧 Programmierung

📰 Google to developers: Our Kotlin-based Jetpack Compose will let you build Android apps faster


📈 30.36 Punkte
📰 IT Nachrichten

🎥 Can I build a Surface Duo app with Jetpack Compose? | One Dev Question


📈 30.36 Punkte
🎥 Video | Youtube

📰 Compose for Wear OS is now 1.0: time to build wearable apps with Compose!


📈 28.43 Punkte
🤖 Android Tipps

🎥 Shaders | Jetpack Compose Tips


📈 25.64 Punkte
🎥 Video | Youtube

🎥 Jetpack Compose: Accessibility


📈 25.64 Punkte
🎥 Video | Youtube

📰 What’s new in the Jetpack Compose August ’23 release


📈 25.64 Punkte
🤖 Android Tipps

🎥 Jetpack Compose: Debugging recomposition


📈 25.64 Punkte
🎥 Video | Youtube

🔧 Mastering @Stable in Jetpack Compose for Better UI Performance


📈 25.64 Punkte
🔧 Programmierung

🎥 How to analyze and improve performance of your Jetpack Compose app


📈 25.64 Punkte
🎥 Video | Youtube

🎥 Jetpack Compose basics | Workshop


📈 25.64 Punkte
🎥 Video | Youtube

📰 A modern approach to Android development, with Jetpack Compose and more!


📈 25.64 Punkte
🤖 Android Tipps

🔧 Android-UI-Toolkit: Jetpack Compose 1.6 legt den Fokus auf höhere Performance


📈 25.64 Punkte
🔧 Programmierung

📰 Now's the time to learn Android development with Jetpack Compose!


📈 25.64 Punkte
🤖 Android Tipps

🎥 Updates to Jetpack Compose libraries, announcements, and more dev news!


📈 25.64 Punkte
🎥 Videos

🎥 Jetpack Compose: Animation


📈 25.64 Punkte
🎥 Video | Youtube

🎥 New in Jetpack Compose, Flutter, TensorFlow, and more Google I/O 2023 announcements


📈 25.64 Punkte
🎥 Videos

🎥 Now in Android: 65 - Android 13 Beta 4, Jetpack Compose 1.2 stable, Wear OS, and more!


📈 25.64 Punkte
🎥 Video | Youtube

📰 Creating a responsive dashboard layout for JetLagged with Jetpack Compose


📈 25.64 Punkte
🤖 Android Tipps

📰 What’s new in Jetpack Compose at I/O ‘24


📈 25.64 Punkte
🤖 Android Tipps

🎥 The winners of the #AndroidDevChallenge for Jetpack Compose


📈 25.64 Punkte
🎥 Video | Youtube

🎥 #AskAndroid at Android Dev Summit 2019 - Jetpack Compose


📈 25.64 Punkte
🎥 Video | Youtube

📰 Android-UI-Toolkit: Jetpack Compose 1.6 legt den Fokus auf höhere Performance


📈 25.64 Punkte
📰 IT Nachrichten

🔧 Implementing Admob Ads With Jetpack Compose


📈 25.64 Punkte
🔧 Programmierung

🎥 Shaders | Jetpack Compose Tips


📈 25.64 Punkte
🎥 Video | Youtube

🎥 Now in Android: 64 - Independent versioning of Jetpack Compose libraries, and more!


📈 25.64 Punkte
🎥 Video | Youtube

🎥 Jetpack Compose: Lists


📈 25.64 Punkte
🎥 Video | Youtube

matomo