Screen brightness is adjusted in many android applications while interacting with the screens. In the application, if we are displaying a QR code in that case we have to increase the screen brightness so that the QR code will be properly visible to the scanner. In this article, we will take a look at How to programmatically increase/decrease the brightness of our android device from our android application 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: Adding permissions in manifest
Navigate to app > manifests > AndroidManifest.xml and add the following permission
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>Step 3: 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.*
import android.os.Bundle
import android.provider.Settings
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.geeksforgeeks.demo.ui.theme.DemoTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DemoTheme {
ControlBrightNess(LocalContext.current)
}
}
}
}
@Composable
fun ControlBrightNess(context: Context) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(all = 30.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
// button to minimize brightness.
Button(onClick = {
// Check whether has the write settings permission or not.
val settingsCanWrite = hasWriteSettingsPermission(context)
// If do not have then open the "can modify system settings" panel
if (!settingsCanWrite) {
changeWriteSettingsPermission(context)
} else {
// brightness = 0
changeScreenBrightness(context, 0)
}
}) {
Text(
text = "Minimize Brightness",
color = Color.White
)
}
Spacer(modifier = Modifier.height(20.dp))
// Button to maximize brightness.
Button(onClick = {
// Check whether has the write settings permission or not.
val settingsCanWrite = hasWriteSettingsPermission(context)
// If do not have then open the "can modify system settings" panel
if (!settingsCanWrite) {
changeWriteSettingsPermission(context)
} else {
// brightness = 100%
changeScreenBrightness(context, 255)
}
}) {
Text(
text = "Maximize Brightness",
color = Color.White
)
}
}
}
// Check whether this app has android write settings permission
fun hasWriteSettingsPermission(context: Context): Boolean {
var ret = true
// Get the result from below code.
ret = Settings.System.canWrite(context)
return ret
}
// Start "can modify system settings" panel
fun changeWriteSettingsPermission(context: Context) {
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS).apply {
data = android.net.Uri.parse("package:${context.packageName}")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
}
// to change the screen brightness
fun changeScreenBrightness(
context: Context,
screenBrightnessValue: Int
) {
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
)
Settings.System.putInt(
context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, screenBrightnessValue
)
}