Learn Android Development

Negative Padding For Jetpack Compose Made Possible

From Bad To Better Solution: Different Ways of Negative Padding for Jetpack Compose

Photo by Sven Mieke on Unsplash

One of the features in Android View XML Layout that is not easily found in Jetpack Compose is Negative Padding. In XML View we can have both positive and negative padding easily.

However, in Jetpack Compose, we can only have Positive Padding, not Negative. 😣

If we have the below

Divider(modifier = Modifier.padding((-8).dp))

there’s no compilation error, unfortunately. It only crashes during runtime when the code is triggered! 😖

java.lang.IllegalArgumentException: Padding must be non-negative

But the good news is, there’re ways to get around it.

1. The Bad Solution: Offset

One of the simplest solutions that come to mind (the first StackOverflow Solution when we Google search “Jetpack Compose Negative Padding”) is using Offset.

--

--