hey! I'm developing a Kotlin app por Wear OS 5.
Now, I have a view with buttons, and I wanna change some of them.
On MyApp.kt, I have the following code
onSleepingButtonPressed = {
context ->
val contentResolver = context.contentResolver
code
try {
// Get current brightness
val currentTimeout = Settings.System.getInt(
contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT
)
Toast.makeText(
context,
"Actual Timeout: $currentTimeout",
Toast.LENGTH_SHORT
).show()
Settings.System.putInt(
contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
when (currentTimeout) {
5000 -> 15000
15000 -> 30000
30000 -> 5000
else -> 5000 // fallback if value is unexpected
}
)
} catch (e: Settings.SettingNotFoundException) {
e.printStackTrace()
}
},
and on SettingsScreen.kt, the following
fun SettingsScreen(context: Context,
uiViewModel: VerisureViewModel,
onRefreshMQTTButtonPressed : () -> Unit,
onMQTTButtonPressed: () -> Unit,
onMQTTEnabledToggled: () -> Unit,
onVibrationButtonPressed : () -> Unit,
onBrigthnessButtonPressed : (Context) -> Unit,
onBrigthnessForceButtonPressed : (Context) -> Unit,
onPermissionButtonPressed : () -> Unit,
onSleepingButtonPressed : (Context) -> Unit,
onManagerButtonPressed : () -> Unit,
onFacerButtonPressed : () -> Unit,
)
Could you change the button to an Inline Slidder? So evey time it's changed, the system setting is changed. When the Inline Slider first appears, it should read the actual value of the setting. Because of that, you should also check if permission for reading that variable (in case it is needed) is already granted, and in case it isn't, ask for it. If it is denied, you should gray out the inline slider in case it is a common and doable thing. If it's not, just hide it. It is really really really important that you use for this androidx.wear.compose.material
Assistant
To replace the Chip with an InlineSlider from androidx.wear.compose.material, we need to:
Read the current SCREEN_OFF_TIMEOUT value when the slider first appears.
Request WRITE_SETTINGS permission if not granted.
Map the timeout values to a discrete slider range (e.g., 5s, 15s, 30s).
Update the system setting when the slider value changes.
Gray out or hide the slider if permission is denied.
✅ Updated Code
1. SettingsScreen.kt – Replace the Chip with InlineSlider
kotlin
User
The slider should offer just int values
Assistant
Here is a minimal, integer-only version of the slider that
• shows the current system timeout (5 s, 15 s or 30 s)
• snaps to the nearest of those three int values
• writes the new value immediately to Settings.System.SCREEN_OFF_TIMEOUT
• is disabled (grayed-out) when the app does not have WRITE_SETTINGS permission.
It uses androidx.wear.compose.material.InlineSlider exactly as requested.
import androidx.wear.compose.material.InlineSlider
import android.provider.Settings
import android.widget.Toast
import androidx.compose.runtime.*
@Composable
fun SettingsScreen(
context: Context,
uiViewModel: VerisureViewModel,
onRefreshMQTTButtonPressed: () -> Unit,
onMQTTButtonPressed: () -> Unit,
onMQTTEnabledToggled: () -> Unit,
onVibrationButtonPressed: () -> Unit,
onBrigthnessButtonPressed: (Context) -> Unit,
onBrigthnessForceButtonPressed: (Context) -> Unit,
onPermissionButtonPressed: () -> Unit,
onSleepingButtonPressed: (Context) -> Unit, // Not used anymore
onManagerButtonPressed: () -> Unit,
onFacerButtonPressed: () -> Unit,
) {
val values = listOf(5000, 15000, 30000)
var currentTimeout by remember { mutableStateOf<Int?>(null) }
var hasPermission by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
hasPermission = Settings.System.canWrite(context)
if (hasPermission) {
currentTimeout = try {
Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
} catch (e: Settings.SettingNotFoundException) {
5000
}
}
}
item {
if (hasPermission && currentTimeout != null) {
val index = values.indexOf(currentTimeout).coerceAtLeast(0)
InlineSlider(
value = index.toFloat(),
onValueChange = { newIndex ->
val newTimeout = values[newIndex.toInt()]
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
newTimeout
)
currentTimeout = newTimeout
Toast.makeText(context, "Timeout set to ${newTimeout / 1000}s", Toast.LENGTH_SHORT).show()
},
steps = values.size - 2,
valueRange = 0f..(values.size - 1).toFloat(),
decreaseIcon = { Icon(Icons.Default.Remove, contentDescription = "Decrease") },
increaseIcon = { Icon(Icons.Default.Add, contentDescription = "Increase") },
enabled = true
)
} else {
InlineSlider(
value = 0f,
onValueChange = {},
steps = 0,
valueRange = 0f..0f,
enabled = false,
decreaseIcon = { Icon(Icons.Default.Remove, contentDescription = null) },
increaseIcon = { Icon(Icons.Default.Add, contentDescription = null) }
)
}
}
}
✅ Notes
Permission Handling: Settings.System.canWrite(context) checks if the app has WRITE_SETTINGS permission. If not, you can trigger onPermissionButtonPressed() to open the system settings.
Graying Out: enabled = false is used to gray out the slider when permission is denied.
Discrete Values: The slider uses steps to snap to 5s, 15s, or 30s.
No More onSleepingButtonPressed: This callback is no longer needed since the slider handles changes directly.
✅ Optional: Trigger Permission Request
If you want to allow the user to grant permission from the UI, you can add a button like:
Let me know if you want to support more timeout values or a continuous slider instead.
import android.content.Context
import android.provider.Settings
import android.widget.Toast
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.wear.compose.material.*
@Composable
fun SettingsScreen(
context: Context,
/* … other parameters … */
) {
// 1. Allowed integer values (ms)
val values = listOf(5_000, 15_000, 30_000)
// 2. State
var currentTimeout by remember { mutableStateOf<Int?>(null) }
val hasPermission = remember { Settings.System.canWrite(context) }
// 3. Read current system value once
LaunchedEffect(Unit) {
if (hasPermission) {
currentTimeout = try {
Settings.System.getInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT
)
} catch (_: Settings.SettingNotFoundException) {
5_000
}
}
}
item {
if (hasPermission && currentTimeout != null) {
// Map the current timeout to an index (0, 1, 2)
val index = values.indexOf(currentTimeout).coerceAtLeast(0)
InlineSlider(
value = index.toFloat(), // float in 0..2
onValueChange = { floatIndex ->
val newIndex = floatIndex.roundToInt()
val newValue = values[newIndex]
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
newValue
)
currentTimeout = newValue
Toast.makeText(
context,
"Timeout set to ${newValue / 1_000} s",
Toast.LENGTH_SHORT
).show()
},
steps = values.size - 2, // 1 step → 3 discrete positions
valueRange = 0f..(values.size - 1).toFloat(),
decreaseIcon = { Icon(Icons.Default.Remove, "Decrease") },
increaseIcon = { Icon(Icons.Default.Add, "Increase") },
enabled = true
)
} else {
// Grayed-out slider when permission is missing
InlineSlider(
value = 0f,
onValueChange = {},
steps = 0,
valueRange = 0f..0f,
decreaseIcon = { Icon(Icons.Default.Remove, null) },
increaseIcon = { Icon(Icons.Default.Add, null) },
enabled = false
)
}
}
}
Key points
• valueRange and steps guarantee only three integer positions (indices 0, 1, 2).
• roundToInt() converts the slider’s float to the exact integer index.