I need to make my own toolbar in Android application. Now it looks like this:
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?
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.
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" .
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.
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.
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"
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:
I won't suggest to go for -ve margins..
Rather setup some background image for button.
Some Important points:-
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 :-
My Result :-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With