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 Implementation
Step 1: Create a New Project in Android Studio
To 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 file
Go 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:
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()
}
}
})
}
}