Home » Android » android – make same space between button in linearlayout

android – make same space between button in linearlayout

Posted by: admin June 15, 2020 Leave a comment

Questions:

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>
How to&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>

Answer:

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>

Answer:

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

Answer:

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

Answer:

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" />

Answer:

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 .

Answer:

try this one

Insert Button Here

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

Insert Button Here