ASH
ASH

Reputation: 1058

buttons taking up same amount of space in android

there has been a few similar question on this topic and i've tried alot of them by now but they don't work. i have three buttons and i want them to take up the same amount of space on the top of the screen. i've tried putting them in in linear layout and using the weights but they don't apear on the graphical layout at all. any suggestions

    <RelativeLayout 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:layout_gravity="top"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".FragActivity" >

        <LinearLayout         
            android:layout_width="match_parent"         
            android:layout_height="0dp"         
            android:orientation="horizontal"         
            android:weightSum="1"         
            android:layout_gravity="top">

            <Button
                android:id="@+id/home"
                android:layout_width="0dp"  
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:onClick="home"
                android:text="Home" />

            <Button
                android:id="@+id/Friends" 
                android:layout_width="0dp" 
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/home"
                android:onClick="Friends"
                android:text="Friends" />

            <Button
                android:id="@+id/makeEvent"
                android:layout_height="wrap_content"
                android:layout_width="0dp"  
                android:layout_weight="0.5"
                android:layout_toRightOf="@+id/home"
                android:onClick="makeEvent"
                android:text="Make Event" />

         </LinearLayout>
    </RelativeLayout>

Upvotes: 0

Views: 66

Answers (3)

Hamid Shatu
Hamid Shatu

Reputation: 9700

Set android:weightSum="3" to the LinearLayout and android:layout_weight="1" to every Button as follows...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_gravity="top"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FragActivity" >

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

        <Button
            android:id="@+id/home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="home"
            android:text="Home" />

        <Button
            android:id="@+id/Friends"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/home"
            android:layout_weight="1"
            android:onClick="Friends"
            android:text="Friends" />

        <Button
            android:id="@+id/makeEvent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="makeEvent"
            android:text="Make Event" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Technivorous
Technivorous

Reputation: 1712

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

        <Button
            android:id="@+id/home"
            android:layout_width="wrap_content"  
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:onClick="home"
            android:text="Home" />

        <Button
            android:id="@+id/Friends" 
            android:layout_width="wrap_content" 
            android:layout_weight="1"
            android:layout_height="wrap_content"

            android:onClick="Friends"
            android:text="Friends" />

        <Button
            android:id="@+id/makeEvent"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"  
            android:layout_weight="1"

            android:onClick="makeEvent"
            android:text="Make Event" />

     </LinearLayout>
</RelativeLayout>

all fixed. your in a linearlayout bud, and you dont set widths and heights, to zero. you have three buttons so set weight sum to 3, and give each button a weight of 1. remove the placement tags, as in a linear layout they will be placed in a linear style.

replace your code with mine and give it a shot

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

You've got the height and width backwards for a horizontal layout. Switch yourLinearLayout to

<LinearLayout         
        android:layout_width="0dp"         
        android:layout_height="match_parent"         
        android:orientation="horizontal"         
        android:layout_gravity="top">

otherwise your height is 0 so of course you won't see anything.

I also removed weight_sum because it isn't needed in this case so it's safer just to let the device calculate for you.

You can also remove android:layout_toRightOf="@+id/home" from your Button because that property doesn't exist for LinearLayout which is what your Button is wrapped in.

Upvotes: 1

Related Questions