1

I have 4 buttons that I space out evenly using layout_weight="1" and layout_width="0dp". However, on large tablet layouts the buttons are too spread out and looks ugly, so I want to set a maxWidth for all of them, and having empty spaces on both sides of the entire LinearLayout instead (so the four buttons are clustered to the center). However, crawling through StackOverflow, many say they don't work together. Is there a way for me to achieve what I want to do above?

tl;dr:

  1. Below a certain width (say, 100dp), all 4 buttons are spaced evenly.
  2. If layout requires buttons to be bigger than 100dp, all 4 buttons are set to 100dp width and stick together, leaving space on both sides of the layout.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/layout_height"
        android:background="@drawable/layout_background"
        android:orientation="horizontal">
    
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/button_background"/>
    
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/button_background"/>
    
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/button_background"/>
    
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/button_background"/>
    
    </LinearLayout>
    
Quorrin
  • 99
  • 1
  • 3
  • 10

2 Answers2

0

Try this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/layout_height"
    android:background="@drawable/layout_background"
    android:orientation="horizontal">

<LinearLayout
   android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal">
<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxWidth="100dp"
    android:background="@color/button_background"/>
</LinearLayout>    

</LinearLayout>

A simple hack could be

  1. Wrap your button with a Linear layout and set weight to this layout

  2. Set max width to button inside this sub layout

0

try this one `

<Button
    android:background="@color/button_background"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:layout_width="0dp" />

<Button
    android:background="@color/button_background"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:layout_width="0dp" />

<Button
    android:background="@color/button_background"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:layout_width="0dp" />

<Button
    android:background="@color/button_background"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:layout_width="0dp" />

`

add margin to each button as you want .

DKV
  • 1,767
  • 3
  • 28
  • 49