Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space between buttons in LinearLayout (Android)

I need to make my own toolbar in Android application. Now it looks like this:enter image description here

So you see spaces between buttons. I tried to put negative margin/padding at buttons, but space didn't disappear.

Here is layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/routes_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.5">
    </ListView>

    <TableRow
        android:id="@+id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:padding="0dp" >

        <Button
            android:id="@+id/btn_routes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:layout_weight="0.2"
            android:layout_marginRight="-10dp"
            android:layout_marginLeft="-10dp"
            android:text="@string/routes"
            android:textSize="10dp" />

        <Button
            android:id="@+id/btn_places"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:layout_weight="0.2"
            android:layout_marginRight="-10dp"
            android:layout_marginLeft="-10dp"
            android:textSize="10dp"
            android:text="@string/places" />

        <Button
            android:id="@+id/btn_events"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:layout_weight="0.2"
            android:layout_marginRight="-10dp"
            android:layout_marginLeft="-10dp"
            android:textSize="10dp"
            android:text="@string/events" />

        <Button
            android:id="@+id/btn_about"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:layout_weight="0.2"
            android:layout_marginRight="-10dp"
            android:layout_marginLeft="-10dp"
            android:text="@string/about"
            android:textSize="10dp" />

    </TableRow>

</LinearLayout>

How to remove space between buttons?

like image 485
Ilya Blokh Avatar asked Feb 26 '12 09:02

Ilya Blokh


People also ask

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.

What is the correct syntax of 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 to divide linear layout in android studio?

If you want to divide layout into two parts of same size you have to set layout_width attributes to 0dp of both layouts and layout_weight to 1 inside the main layout. Then set weightSum to 2 of the parent layout. Same concept can be applied to divide layout to any ratio.

How views are aligned inside a LinearLayout?

If you create a horizontal layout, child views will be aligned horizontally, Child views are vertically aligned if you set it to vertical. You can also change this orientation dynamically at runtime of the program by using setOrientation() method. The default way is to be align horizontally.


3 Answers

Try changing the color of the button, because the default interface of the button which is native to android is actually smaller than it's size, and it's center-fitted to make it look cool.

Change it's background to black or something and you'll see the real size of the button.

android:background="#000"

like image 163
Sami Avatar answered Sep 27 '22 10:09

Sami


Here's a solution. It seems that I hurried to post a question here.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

    <ListView
        android:id="@+id/routes_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.5">
    </ListView>

    <TableRow
        android:id="@+id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_marginBottom= "-7dp">

        <Button
            android:id="@+id/btn_routes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:layout_marginRight="-4.5dp"
            android:layout_marginLeft="-5dp"
            android:text="@string/routes"
            android:textSize="10dp" />

        <Button
            android:id="@+id/btn_places"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:layout_marginRight="-4.5dp"
            android:layout_marginLeft="-4.5dp"
            android:textSize="10dp"
            android:text="@string/places" />

        <Button
            android:id="@+id/btn_events"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:layout_marginRight="-4.5dp"
            android:layout_marginLeft="-4.5dp"
            android:textSize="10dp"
            android:text="@string/events" />

        <Button
            android:id="@+id/btn_about"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:layout_marginRight="-4dp"
            android:layout_marginLeft="-4.5dp"
            android:text="@string/about"
            android:textSize="10dp" />

    </TableRow>

</LinearLayout>

Result: enter image description here

like image 39
Ilya Blokh Avatar answered Sep 27 '22 10:09

Ilya Blokh


I won't suggest to go for -ve margins..

Rather setup some background image for button.

Some Important points:-

  1. The background image itself should not contain any white space at borders.
  2. You have to create a selector file for that button after providing background, in which you will describe which image to show in at different states of button likewise selected,focused etc. other wise you will not be able to see the effects which appear by default (like silver button and when it is clicked it gets orange)

Solutions :-

  1. How to create a selector you can refer this lik
  2. I have used this button in my case

My Result :-

enter image description here

like image 1
DeltaCap019 Avatar answered Sep 23 '22 10:09

DeltaCap019