make same space between button in linearlayout
Asked Answered
A

7

9

i have button in that i want to put same space between all button so if i run app in tablet the space between button will equal divide, i am using linearlayout, i know there is layout_weight option but i don't want to stretch icon

for example enter image description here

my xml code is below

  <LinearLayout
        android:id="@+id/shareLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnRequestAPrescriptionRefill"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/btnFacebook"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_facebook" />

        <Button
            android:id="@+id/btnYoutube"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_youtube" />

        <Button
            android:id="@+id/btnTwitter"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_twitter" />

        <Button
            android:id="@+id/btnPintrest"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_pintrest" />
    </LinearLayout>
Aweigh answered 22/2, 2013 at 9:26 Comment(0)
A
31

You can use layout_weight. But like you said Buttons will be stretched, so instead of using weight on Buttons, use on spaces(Views).

    <Button
        android:id="@+id/btnFacebook"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_facebook" />

    <View
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btnYoutube"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_youtube" />

    <View
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btnTwitter"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_twitter" />

    <View
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btnPintrest"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_pintrest" />
</LinearLayout>
Alloy answered 22/2, 2013 at 9:36 Comment(3)
Finally your answer is fit :)Aweigh
Thank you so much Archie.bpgcAweigh
good answer. Small improvement: use a <Space android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" > </Space> instead of a <View> to make your objective clearer in your XML.Gentianaceous
G
8

To make your intent clearer in your XML use a <Space> instead of a <View> as follows:

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/Blue"
        android:orientation="horizontal" >

       <Space
           android:layout_width="0dp"
           android:layout_height="1dp"
           android:layout_weight="1" >
       </Space>

       <Button
           android:id="@+id/btnFacebook"
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:background="@drawable/btn_facebook" />

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" >
        </Space>

...and repeat with the rest of your buttons...

</LinearLayout>
Gentianaceous answered 2/9, 2014 at 14:58 Comment(0)
Y
4

give layout_weight to every Button 1, it will divide all space

Yungyunick answered 22/2, 2013 at 9:27 Comment(0)
C
4

Try this...

    <Button
        android:id="@+id/btnFacebook"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_facebook" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3" />

    <Button
        android:id="@+id/btnYoutube"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_youtube" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3" />

    <Button
        android:id="@+id/btnTwitter"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_twitter" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"/>

    <Button
        android:id="@+id/btnPintrest"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_pintrest" />
Cod answered 22/2, 2013 at 9:29 Comment(2)
no deepzz i have tried your code but it will look same as layout_weight="1"Aweigh
@SiddhpuraAmit : Try giving width 0dp instead of wrap_content for those layoutsCod
P
4

replace every button with

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    >
    <Button
        android:id="@+id/btnFacebook"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_facebook" />
</RelativeLayout>

i.e. wrap every button with RelativeLayout with android:layout_weight and android:gravity

Prefigure answered 22/2, 2013 at 9:33 Comment(0)
H
3

I would go with a table Layout and have strechColums="0" . Else if you want to stick to linear layout then have you tried out space .

Hardspun answered 22/2, 2013 at 9:29 Comment(1)
Hello Chris can u suggest with strechColumns how can i do itAweigh
P
1

try this one

Insert Button Here

<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />

Insert Button Here

Pseudohemophilia answered 24/7, 2019 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.