freeman292
freeman292

Reputation: 47

Android: How to change divide the screen in 3 equal buttons

I have 3 buttons in a layout. What I want to do is increase the size of the buttons so they fill the entire screen. How can I do that in the xml file?

Upvotes: 1

Views: 2056

Answers (3)

marduc812
marduc812

Reputation: 1205

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_weight="1"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_weight="1"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button3"
    android:layout_weight="1"/>
</LinearLayout>

You need weightSum

Upvotes: 5

Alon Levanon
Alon Levanon

Reputation: 701

You need to put the buttons inside a LinearLayout and use weight:

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:weightSum="3">
<Button 
    android:layout_height="0dip"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    />
<Button 
    android:layout_height="0dip"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    />
<Button 
     android:layout_height="0dip"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    />

Upvotes: 0

Carlos J
Carlos J

Reputation: 3045

You can use the weight property of a LinearLayout. I'm gonna make a guess and assume you want the buttons in a vertical layout.

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

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

The Buttons will always fill the screen and you can play with the weight value to modify their height

Upvotes: 1

Related Questions