Torch App in Android using Jetpack Compose Last Updated : 20 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Nowadays almost all mobile devices come with a flashlight that is used by the camera. Along with that, this same flashlight is also used as a torch on android devices. For using this flashlight there are many android applications named Torch to use this flashlight as a torch. In this article, we will be creating a simple torch application in android using Jetpack Compose.Step by Step ImplementationStep 1: Create a New Project in Android StudioTo create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.Step 2: Working with the MainActivity.kt fileGo to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.MainActivity.kt: Kotlin package com.geeksforgeeks.demo 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.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.* import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.* import androidx.compose.ui.unit.* import com.geeksforgeeks.demo.ui.theme.DemoTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { DemoTheme { TorchApplication(LocalContext.current) } } } } @Composable fun TorchApplication(context: Context) { val isTorchOn = remember { mutableStateOf(false) } val torchMode = remember { mutableStateOf("Off") } Column( modifier = Modifier .fillMaxSize() .padding(all = 30.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, ) { Text( text = "Torch is " + torchMode.value, color = Color.Black, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Default, fontSize = 20.sp, modifier = Modifier.padding(5.dp) ) // switch to toggle torch on/off Switch(checked = isTorchOn.value, onCheckedChange = { isTorchOn.value = it lateinit var cameraID: String val cameraManager: CameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager try { // O -> back camera, // 1 -> front camera // since, torch is in back camera, let's use '0' cameraID = cameraManager.cameraIdList[0] } catch (e: Exception) { e.printStackTrace() } if (isTorchOn.value) { try { // To switch on torch, set torch mode to "true" cameraManager.setTorchMode(cameraID, true) Toast.makeText(context, "Torch turned on..", Toast.LENGTH_LONG).show() torchMode.value = "On" } catch (e: Exception) { e.printStackTrace() } } else { // when switch is off try { // To switch off torch, set torch mode to "false" cameraManager.setTorchMode(cameraID, false) Toast.makeText(context, "Torch turned off..", Toast.LENGTH_SHORT).show() torchMode.value = "Off" } catch (e: Exception) { e.printStackTrace() } } }) } } Output: Comment More infoAdvertise with us Next Article Torch App in Android using Jetpack Compose C chaitanyamunje Follow Improve Article Tags : Kotlin Android Android-Jetpack Similar Reads Text in Android using Jetpack Compose Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. Android Text is a simple view in Android which is used to display text inside our App. In this article, we will take a look at the implementation of Text in Android using Jetpack Compose. Importan 5 min read TopAppBar in Android using Jetpack Compose TopAppBar is similar to that of the Action Bar widget in Android. This is one of the most UI components in Android. The action bar is used to represent the app name and action items in Android. In this article, we will take a look at the implementation of the TopAppBar in Android using Jetpack compo 3 min read Services in Android using Jetpack Compose Services in Android applications are used to perform some background tasks within android applications. We can call the service to perform a specific task in the background of our application. That task will be performed continuously if our application is not visible to the user. Services are genera 6 min read Switch Button in Android using Jetpack Compose A Switch or a Switch Button in Android is a UI element that is used to switch between two states upon click. It can be assumed as a Boolean button with two different values. Some states where you may find a Switch in your Android device can be WIFI ON and OFF, Bluetooth ON and OFF, Dark Mode and Lig 2 min read Proximity Sensor in Android App using Jetpack Compose Proximity Sensor is one of the sensors present in the mobile device which is used by everyone using smartphone. This sensor is present in mobile devices under the earpiece section. This sensor is used within the mobile device when the user is attending a call to prevent the disconnection of the call 6 min read Like