Denis Vitez
Denis Vitez

Reputation: 648

How to make LinearLayout fill available space when some elements are not set

In my activity I have LinearLayout with 4 elements:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/linearLayout2">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton1"
        android:layout_weight=".25"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:background="@null" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton2"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_weight=".25"
        android:background="@null" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton3"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_weight=".25"
        android:background="@null" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton4"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_weight=".25"
        android:background="@null" />

</LinearLayout>

When I have all 4 elements set the layout is rendered as following: Result when all 4 elements are set

Which is as I want it. The problem occurs when one or more of the elements are not present and I get the following result:

Result when only 3 elements are set

How can I make my elements spread over all available space? So when only 2 elements have their background set the result should look something like: Expected result when 2 elements are set

Instead of how it looks now: enter image description here

Upvotes: 0

Views: 903

Answers (2)

curiousMind
curiousMind

Reputation: 2812

you have to use weight so when no view will be there it will adjust automatically

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout2">
<ImageButton
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:id="@+id/imgButton1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:background="@null" />

<ImageButton
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/imgButton2"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:background="@null" />

<ImageButton
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/imgButton3"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
     android:background="@null" />

<ImageButton
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/imgButton4"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:background="@null" />

 </LinearLayout>

Upvotes: 2

Dhaval Patel
Dhaval Patel

Reputation: 10299

Here android:gravity="center" will align all the child view in center,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"    
    android:layout_alignParentBottom="true"
    android:id="@+id/linearLayout2">

   ... your code

</LinearLayout>

Upvotes: 0

Related Questions