Search code examples
androidkotlinandroid-constraintlayout

Can't remove margin with guidline


I've simple view, fragment container button on the bottom side. I've used guidline horizontal and now I've margin on the top and on the bottom of buttons. I don't see this margins in the code, so why I've this margins and how to remove it ?

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <fragment
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:name="com.package.list.ListFragment"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </fragment>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9" />

    <Button
        android:id="@+id/list_btn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="List"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • You will use a command inside Android Studio called go to declaration which is cmd+B on a Mac or ctrl+B on Windows. I will just use ctrl+B for simplicity.

    Answer

    I think it's because of the default style that is applied to buttons. If you search for it in Android Studio you can actually see that Button style contains a background like this:

    <item name="android:background">@drawable/abc_btn_default_mtrl_shape</item>
    

    And then when you ctrl+B this file you will see something called insetTop and insetBottom

    <inset 
        android:insetTop="@dimen/abc_button_inset_vertical_material"
        android:insetBottom="@dimen/abc_button_inset_vertical_material">
    

    Again, ctrl+B inside and you finally see that they are both equal to 6dp. So in you main activity, all you need to do, is set the insetTop and insetBottom to 0dp like this:

    <Button
        android:id="@+id/list_btn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        android:text="List"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />