Chris James Hancocks
Chris James Hancocks

Reputation: 323

Buttons all squashing onto one row

I have a row of buttons and I want them to all display in a line if turned to landscape and then fit as many as it can when portrait and move other buttons to below. At the moment the buttons are just squashing together on one row.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
android:gravity="center_horizontal">

<Button 
    android:id="@+id/Button02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/analoguebutton" />

<Button 
    android:id="@+id/Button03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp"
    android:background="@drawable/analoguebutton" />

<Button 
    android:id="@+id/Button04" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp"
    android:background="@drawable/analoguebutton" />

All squashed

Upvotes: 2

Views: 200

Answers (2)

Rishabh Srivastava
Rishabh Srivastava

Reputation: 3745

Just change android:layout_width="wrap_content" to android:layout_width="match_parent" in all buttons and give android:layout_weight="1" to all the three buttons. Hope it works.

Upvotes: 2

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Make two layouts with the same name.
Put the "vertical" one in res/layout-port and the "horizontal" one in res/layout-land.
Design the two layouts to match your tastes.
Android will automatically choose this or that layout accordingly to the device's orientation.

Upvotes: 2

Related Questions