androidlayoutandroid-flexboxlayout

How to use weight in FlexboxLayout to center 3 linear layouts?


I have a FlexboxLayout and inside it I have 3 LinearLayout's that I want to center with the same width and height, however they are with different width's. How can I center they in FlexboxLayout?

Here is my XML code:

 <com.google.android.flexbox.FlexboxLayout
                                android:visibility="@{TextUtils.isEmpty(listing.phone) &amp;&amp; TextUtils.isEmpty(listing.email) &amp;&amp; TextUtils.isEmpty(listing.url) ? View.GONE : View.VISIBLE}"
                                android:layout_width="match_parent"
                                android:layout_height="100dp"
                                android:layout_weight=""
                                android:paddingBottom="16dp"
                                android:orientation="horizontal">

                                <LinearLayout
                                    android:visibility="@{TextUtils.isEmpty(listing.phone) ? View.GONE : View.VISIBLE}"
                                    tools:visibility="visible"
                                    app:layout_flexGrow="1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:clickable="true"
                                    android:onClick="@{() -> handlers.onClickCall(listing.phone)}"
                                    android:gravity="center"
                                    android:orientation="vertical">

                                    <android.support.v7.widget.AppCompatImageView
                                        android:id="@+id/imageView2"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:background="@drawable/rounded"
                                        android:backgroundTint="@color/listing"
                                        app:backgroundTint="@color/listing"
                                        android:padding="16dp"
                                        app:srcCompat="@drawable/ic_call"
                                        android:tint="#fff" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="8dp"
                                        android:text="@string/call_us"
                                        android:textSize="12sp" />

                                </LinearLayout>

                                <LinearLayout
                                    android:id="@+id/buttonSendEmail"
                                    android:visibility="@{TextUtils.isEmpty(listing.email) ? View.GONE : View.VISIBLE}"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    app:layout_flexGrow="1"
                                    tools:visibility="gone"
                                    android:gravity="center"
                                    android:orientation="vertical">

                                    <android.support.v7.widget.AppCompatImageView
                                        android:id="@+id/buttonEmail"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:background="@drawable/rounded"
                                        android:backgroundTint="@color/listing"
                                        app:backgroundTint="@color/listing"
                                        android:padding="16dp"
                                        app:srcCompat="@drawable/ic_mail"
                                        android:tint="#fff" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="8dp"
                                        android:text="@string/send_email"
                                        android:textSize="12sp" />

                                </LinearLayout>

                                <LinearLayout
                                    android:visibility="@{TextUtils.isEmpty(listing.url) ? View.GONE : View.VISIBLE}"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    app:layout_flexGrow="1"
                                    tools:visibility="gone"
                                    android:gravity="center"
                                    android:clickable="true"
                                    android:onClick="@{() -> handlers.onClickWebsite(listing.url)}"
                                    android:orientation="vertical">

                                    <android.support.v7.widget.AppCompatImageView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:background="@drawable/rounded"
                                        android:backgroundTint="@color/listing"
                                        app:backgroundTint="@color/listing"
                                        android:padding="16dp"
                                        app:srcCompat="@drawable/ic_internet"
                                        android:tint="#fff" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="8dp"
                                        android:text="@string/visit_website"
                                        android:textSize="12sp" />

                                </LinearLayout>

                            </com.google.android.flexbox.FlexboxLayout>

Solution

  • If you plan to use FlexBox you can try to set layout_width of each LinearLayout to 0dp and with the app:layout_flexGrow="1" all LinearLayout will expand equally to accommodate all space left:

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_flexGrow="1"
        ...>