Time & configuration · RemoteConfiguration
The player's window, density and font size. Change the viewport.
Open the live, interactive demo →
Change its controls, read the remote document it produces and the diff after every change.
What it is
Things only the device knows: the current time and date, and the screen's size, density and text size.
Good for
- Clocks and countdowns
- Layouts that adapt to the screen
- Respecting the user's font size
Code
The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.
import androidx.compose.remote.creation.compose.layout.RemoteArrangement
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteColumn
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteRow
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.state.RemoteConfiguration
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun Configuration() {
// RemoteConfiguration is the player's display, known only at play time.
val config = RemoteConfiguration
RemoteColumn(verticalArrangement = RemoteArrangement.spacedBy(4.rdp)) {
RemoteText(
"window: ".rs +
config.windowWidthPx.toRemoteString() +
" × ".rs +
config.windowHeightPx.toRemoteString() +
" px".rs,
)
RemoteText("density: ".rs + config.density.toRemoteString())
RemoteText("fontSize: ".rs + config.fontSizePx.toRemoteString() + " px".rs)
// The window size as dp drives layout: a bar half the window wide.
RemoteBox(
RemoteModifier.size(config.windowWidth / 2, 12.rdp).background(Color(0xFF1E88E5).rc),
)
// …a square the size of the user's font size, and a bar a tenth of the window high.
RemoteRow(horizontalArrangement = RemoteArrangement.spacedBy(8.rdp)) {
RemoteBox(
RemoteModifier.size(config.fontSize, config.fontSize)
.background(Color(0xFFFB8C00).rc),
)
RemoteBox(
RemoteModifier.size(24.rdp, config.windowHeight / 10)
.background(Color(0xFF8E24AA).rc),
)
}
}
}
API
creation.compose.state.RemoteTimeVariables
class RemoteTimeVariables
RemoteFloat animationDeltaTime
RemoteFloat animationTime
RemoteFloat continuousSeconds
RemoteInt dayOfMonth
RemoteInt dayOfWeek
RemoteInt dayOfYear
RemoteInt epochSecond
RemoteInt hourOfDay
RemoteInt minuteOfDay
RemoteInt month
RemoteInt secondOfHour
RemoteInt utcOffsetSeconds
RemoteInt year
RemoteTimeVariables INSTANCEcreation.compose.state.RemoteConfiguration
class RemoteConfiguration
RemoteFloat density
RemoteDp fontSize
RemoteFloat fontSizePx
RemoteDp windowHeight
RemoteFloat windowHeightPx
RemoteDp windowWidth
RemoteFloat windowWidthPx
RemoteConfiguration INSTANCEcreation.compose.text.RemoteTimeDefaults
class RemoteTimeDefaults
RemoteString defaultTimeString(optional RemoteBoolean is24HourFormat)
RemoteBoolean is24HourFormat()
RemoteTimeDefaults INSTANCE