Ido Wolf
Ido Wolf

Reputation: 181

Android LinearLayout - wrap_content + Split remaining space

Consider the following layout: a horizontal LinearLayout with 2 buttons with the text "Push me", and an ImageButton with a small envelope icon (for example, to send an email).

I want the ImageButton's width to be set to "wrap_content" so it's only as large as the envelope's size, and then I want the other two buttons to evenly split the remainder of the screen evenly. How can it be done?

Upvotes: 0

Views: 382

Answers (2)

Ivan
Ivan

Reputation: 297

layout_weight specifies how much of the extra space in the layout to be allocated to the View. So, for your envelope ImageButton set the android:layout_width="wrap_content". And for your remaining 2 buttons set android:layout_width="0dp" and add android:layout_weight="0.5" to both buttons.

Upvotes: 1

Prasad
Prasad

Reputation: 3562

Try this

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <ImageButton
    android:id="@+id/ib"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>

  <LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Push me"/>

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Push me"/>

  </LinearLayout>
</LinearLayout>

Upvotes: 0

Related Questions