2

I have a LinearLayout and its XML is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Button" />
</LinearLayout>

and it is the result is:

enter image description here

as you can see the top left button has a little margin from top but as the code shows there are no margins. why is this happening?

also there is a weird solution which is if you set gravity:top to all buttons you will get the expected result. but why is it even needed because linearlayout(horiz) should start adding items from top left to top right.

ClassY
  • 744
  • 5
  • 20

3 Answers3

2

I was referring some documents and SO threads to look for the solution as the question seemed very intresting to me.

Finally I have found out the reason.

A horizontal LinearLayout aligns the baselines of all its child controls by default. So the first line of text in your multi-line button is vertically aligned with the single line of text in the other buttons.

To disable this behaviour, set android:baselineAligned="false" on the LinearLayout.

All credit goes to @Karu for this answer : https://stackoverflow.com/a/8290258/4211264

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
0

This is due to text wrapping in first button. Other two buttons take too match space on screen. And first button try's to stay on screen by self wrapping. I don't know what is your task. If you want to keep buttons in one line try to use layout_weight = 1 on all buttons.

  • Hi. Thank you for your response. the heights are set to wrap. so shouldn't this stretch the button vertically till it can fit? if that is the case why is this getting a margin from above since that has nothing to do with its width – ClassY May 28 '21 at 10:50
0

This is because you have given different weights to every button. You need to give equal weight to every button. Replace your layout file code with this.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
tools:context=".MainActivity">

<Button
    android:id="@+id/button3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />
paul035
  • 327
  • 2
  • 8
  • 1
    Hi. thank you for your response. but this will give equal ''width" to all direct children. my problem is the margin it gets from the top which is nowhere to be found. – ClassY May 28 '21 at 10:48
  • OP has asked for the reason of the extra margin at top in first button. And based on the requirement it may be necessary to give differrent weights to all the widgets – Bhargav Thanki May 28 '21 at 10:50