Search code examples
androidxmljustify

How to justify the content in Android Studio - XML


I would like to know if it is possible to justify the content of a LinearLayout in Android Studio as I usually do for web.

Web CSS

.space-around {
  display: flex;
  justify-content: space-around;
}

What space-around does is to evenly distribute in the line with equal space around them making the content to fit into the div (link)

How could I get the same effect in Android Studio in my LinearLayout?

Android

  <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:orientation="horizontal">
  </LinearLayout>

Solution

  • Although this does not fully answer your question because it is not using LinearLayout, but Google has provided an Android implementation of the so-called flexbox layout, which has a lot of settings which you might already be familiar with from CSS.

    It also supports justify-content, if you scroll down through the readme there are a lot of examples included.

    To use this, you will have to add a dependency on it, of course. Use version 1.0.0 if you are not on AndroidX yet, otherwise just use the latest stable one.