Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make same space between button in linearlayout

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>
like image 802
Siddhpura Amit Avatar asked Feb 22 '13 09:02

Siddhpura Amit


People also ask

Is it possible to evenly distribute buttons across the width of an android LinearLayout?

Expanding on fedj's answer, if you set layout_width to 0dp and set the layout_weight for each of the buttons to 1, the available width will be shared equally between the buttons.

How do you make a space between LinearLayout children?

You can add at the end of each text "\n" to split a line and make space between elements.

How do you give a space between two buttons in android linear layout?

2 answers. In addition to the buttons you will need to use an view "empty" (1) to represent the spaces before, between and after each button. layout_weight attribute is used to scale the spaces equally.

How do you put a space in a linear layout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .


2 Answers

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>
like image 126
Archie.bpgc Avatar answered Sep 17 '22 06:09

Archie.bpgc


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>
like image 43
PeteH Avatar answered Sep 21 '22 06:09

PeteH