Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to make it no space between 2 button in a horizontal linearlayout

I just found I cannot remove the space between 2 buttons even if I set the layout_marginRight and layout_marginLeft as below. But it make sense if I set the space larger such as 10 dp. Any way to solve it?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal" android:padding="0dp"
    android:layout_height="wrap_content" android:gravity="fill_horizontal" android:layout_margin="0dp">
    <Button android:id="@+id/LocationTitleButton"
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:layout_marginRight="0dp"
                    android:layout_gravity="center_vertical" 
                    android:layout_weight="1"
                    android:ellipsize="end" 
                    android:gravity="center_vertical"
                    android:scrollHorizontally="true" 
                    android:singleLine="true"
                    android:text="Add location" 
                    android:textStyle="bold" />
                <Button android:textColor="#FF000000" 
                    android:layout_weight="0" 
                    android:id="@+id/AddLocationButton" 
                    android:text="Search" 
                    android:gravity="center_vertical"
                    android:layout_gravity="center_vertical" 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" 
                    android:layout_marginLeft="0dp" />
</LinearLayout>
like image 796
George_BJ Avatar asked Nov 12 '11 14:11

George_BJ


People also ask

How do you do a horizontal LinearLayout?

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

How do I center a button horizontally on android?

If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.

How do you align text in LinearLayout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”.

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.


1 Answers

Please look at Bryan's answer. With my answer both buttons overlap. Bryans answer shows the real size of the buttons.

Old answer:

Just set the android:layout_marginRight of the first button to "-8dip" or even more. Than the space between the two buttons will get smaller.

like image 194
Franziskus Karsunke Avatar answered Oct 03 '22 07:10

Franziskus Karsunke