visibility
The middle box in its three visibility states.
Open the live, interactive demo →
Change its controls, read the remote document it produces and the diff after every change.
What it is
Shows or hides an element, either keeping its space or letting the layout close the gap. The device can switch it without a new screen from the server.
Good for
- Expandable sections
- Optional badges
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.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteRow
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.modifier.visibility
import androidx.compose.remote.creation.compose.state.RemoteInt
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun Visibility(visibility: Int) {
RemoteRow(verticalAlignment = RemoteAlignment.CenterVertically) {
RemoteBox(RemoteModifier.size(60.rdp).background(Color(0xFF90A4AE).rc))
// 1 = visible, 2 = invisible (keeps its space), 0 = gone (takes no space).
RemoteBox(
RemoteModifier.visibility(RemoteInt(visibility))
.size(60.rdp)
.background(Color(0xFFE53935).rc),
)
RemoteBox(RemoteModifier.size(60.rdp).background(Color(0xFF90A4AE).rc))
}
}
API
creation.compose.modifier.visibility
RemoteModifier visibility(RemoteModifier, RemoteInt visible)