https://kotlinlang.org logo
j

Julius Marozas

07/13/2020, 4:56 PM
Hi, quick question. I have a
LinearProgressIndicator
below an app bar. Can I hide the progress indicator without affecting the content below it? I am looking for something like
View.INVISIBLE
(and not
View.GONE
).
a

Alejandro Rios

07/13/2020, 4:57 PM
maybe put it inside a
Box
and hide the
Box
?
z

Zach Klippenstein (he/him) [MOD]

07/13/2020, 4:58 PM
You could also just apply
Modifier.opacity(0f)
to your indicator.
👍 1
j

Julius Marozas

07/13/2020, 5:04 PM
Thanks for the quick replies! I was able to do this both ways: with `Box`:
Copy code
Box(modifier = Modifier.height(ProgressIndicatorConstants.DefaultStrokeWidth)) {
    if (showProgress)
        LinearProgressIndicator(modifier = Modifier.fillMaxWidth()
}
with `opacity`:
Copy code
LinearProgressIndicator(modifier = Modifier.fillMaxWidth().drawOpacity(if (showProgress) 1f else 0f))
The
opacity
approach is a little bit simpler. But I think something like
View.INVISIBLE
would also be useful.
👍🏼 2
z

Zach Klippenstein (he/him) [MOD]

07/13/2020, 5:07 PM
Could be as simple as
fun Modifier.visible(visible: Boolean) = drawOpacity(if (visible) 1f else 0f)
, although there’s probably also some semantics/input considerations that it should take into account (e.g. disabling input, marking as hidden for accessibility, etc).
Progress indicators don’t handle input events so for your case that part doesn’t matter.
t

Timo Drick

07/14/2020, 7:39 PM
Here is a simple Visibility modifier implementation:
Copy code
enum class Visibility {
    VISIBLE, INVISIBLE, GONE
}

@Composable
fun Modifier.visibility(visibility: Visibility) = this + object : LayoutModifier {
    override fun MeasureScope.measure(
        measurable: Measurable,
        constraints: Constraints,
        layoutDirection: LayoutDirection
    ): MeasureScope.MeasureResult = if (visibility == Visibility.GONE) {
        layout(0, 0) {
            // Empty placement block
        }
    } else {
        val placeable = measurable.measure(constraints)
        layout(placeable.width, placeable.height) {
            if (visibility == Visibility.VISIBLE) {
                placeable.place(0, 0)
            }
        }
    }
}
👍 1
z

Zach Klippenstein (he/him) [MOD]

07/14/2020, 7:57 PM
What’s that
constraints.offset()
call for in the
else
block?
t

Timo Drick

07/14/2020, 7:59 PM
upps i think this was just a test. It should not do anything
Just remove this line
👍 1
z

Zach Klippenstein (he/him) [MOD]

07/14/2020, 8:00 PM
Nice!
2 Views