put space between android buttons
Asked Answered
N

9

22
<Button
    android:id="@+id/o_pharmacy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/p2"
    android:text="@string/o_pharmacy"
    android:textSize="26sp" />

<Button
    android:id="@+id/lab"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/lab"
    android:text="@string/lab"
    android:textSize="26sp" />

<Button
    android:id="@+id/i_pharmacy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/p1"
    android:text="@string/i_pharmacy"
    android:textSize="26sp" />

I tried above code to display 3 buttons in Liner layout. It works but I need to put space between the two buttons.

Nebraska answered 3/8, 2013 at 4:51 Comment(1)
what is the orientation of your LinearLayout ?Comminute
C
34
android:layout_margin="10dp"

for each button

Catabolism answered 3/8, 2013 at 4:53 Comment(0)
C
21

If the orientation of your LinearLayout is vertical, use

android:layout_marginTop="10dp"

otherwise, use

android:layout_marginLeft="10dp"
Comminute answered 3/8, 2013 at 4:55 Comment(0)
S
4
<Button
    android:id="@+id/o_pharmacy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/p2"
    android:text="@string/o_pharmacy"
    android:textSize="26sp" />

<Button
    android:id="@+id/lab"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="25dp"
    android:drawableLeft="@drawable/lab"
    android:text="@string/lab"
    android:textSize="26sp" />

<Button
    android:id="@+id/i_pharmacy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="25dp"
    android:drawableLeft="@drawable/p1"
    android:text="@string/i_pharmacy"
    android:textSize="26sp" />

Try this.

Sesquicentennial answered 3/8, 2013 at 4:54 Comment(0)
U
2

The easiest way to make space between 3 buttons in an horizontal LinearLayout to use this on the center button:

android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"

If you have vertical LinearLayout, you can use marginTop and marginBottom.

Unbodied answered 8/7, 2016 at 10:18 Comment(0)
B
1

The best is to use android:layout_marginTop="10dp" in your XML activity as this gives accurate spacing between that button and the other button or widget. Repeat this for rest of the buttons. Happy Programming!

Blackstone answered 8/7, 2016 at 9:48 Comment(1)
Is this a repeat of this existing answer?Mayne
P
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
                android:id="@+id/o_pharmacy"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:layout_marginTop="10dip"

                android:drawableLeft="@drawable/p2"
                android:text="@string/o_pharmacy"
                android:textSize="26sp" />

                   <Button
                   android:id="@+id/lab"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"

                   android:layout_marginTop="10dip"

                   android:drawableLeft="@drawable/lab"
                   android:text="@string/lab"
                   android:textSize="26sp" />

                   <Button
                   android:id="@+id/i_pharmacy"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"

                   android:layout_marginTop="10dip"

                   android:drawableLeft="@drawable/p1"
                   android:text="@string/i_pharmacy"
                   android:textSize="26sp" />

I am assuming that you are using a Vertical orientation for your LinearLayout, otherwise this code won't make sense as your buttons are Fill_parent for layout_width. Notice the line that says android:layout_marginTop="10dip" which ensures that you leave a reasonable 10 dip space in between your buttons. Ofcourse, you can increase (or decrease) that space in between your buttons. That's your choice.

Hope this answered your question satisfactorily.

Prothrombin answered 3/8, 2013 at 5:17 Comment(0)
M
0
 android:layout_marginBottom="50dp"

 android:layout_marginTop="50dp"
Maite answered 3/8, 2013 at 5:47 Comment(0)
H
0

I think you can try layout_weight. If you need 3 in a row with some space in between. you can do like this

<LinearLayout
                  android:id="@+id/buttons"
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"

                  >

                    <com.google.android.material.button.MaterialButton
                      android:id="@+id/o_pharmacy"
                      android:layout_width="0dp"
                      android:layout_height="64dp"
                      android:layout_weight="1"
                      android:layout_marginLeft="16dp"
                      android:text="@string/o_pharmacy"


                      />

                    <com.google.android.material.button.MaterialButton
                      android:id="@+id/lab"
                      android:layout_width="0dp"
                      android:layout_height="64dp"
                      android:layout_weight="1"
                      android:layout_marginLeft="16dp"
                      android:padding="5dp"
                      android:text="@string/lab"


                      />

                    <com.google.android.material.button.MaterialButton
                      android:id="@+id/i_pharmacy"
                      android:layout_width="0dp"
                      android:layout_height="64dp"
                      android:layout_weight="1"
                      android:layout_marginLeft="16dp"
                      android:layout_marginRight="16dp"
                      android:text="@string/i_pharmacy"


                      />

                </LinearLayout>
Hunterhunting answered 21/8, 2019 at 5:1 Comment(0)
P
-1

It's better to use

 android:textSize="26dp"

instead of

android:textSize="26sp"

to have a better result in all devices of varied sizes!

Pressing answered 3/8, 2013 at 5:6 Comment(1)
Nope. sp is required for text sizesTunisia

© 2022 - 2024 — McMap. All rights reserved.