Hubit
Hubit

Reputation: 27

Material Button not filling whole width of screen

I am pretty new to the whole app development stuff, and I'm just trying to learn the basics. Especially with XML I only have very little experience. My problem is, that I want to add a button, which expands over the whole width of the screen, but it just won't work and leaves some kind of margin around it. I have already tried a lot, but I couldn't find any solution, or maybe I just didn't know what to exactly search for. One tip I found with the android:insets attributes didn't work, except for top and bottom insets.

enter image description here

This is my component tree:

enter image description here

And this is the XML:

<?xml version="1.0" encoding="utf-8"?>
    <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"
        android:background="@drawable/bg_2"
        tools:context=".ApplicationActivity">
    
        <com.google.android.material.button.MaterialButton
            android:id="@+id/backBtn"
            android:layout_width="450dp"
            android:layout_height="59dp"
            android:background="@drawable/back_btn_bg"
            android:fontFamily="@font/montserrat_thin"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            android:text="@string/back_btn"
            app:backgroundTint="#470000"
            app:cornerRadius="0dp"
            app:fontFamily="@font/montserrat_thin"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

I hope I included every information that's needed. Thank you in advance :)

Upvotes: 0

Views: 961

Answers (2)

che10
che10

Reputation: 2318

Just change the layout_width as 0dp so that it can take whole available space between the constraints.

android:layout_width="0dp"

I also removed the insetRight and insetLeft tag. Here is the updated XML

<com.google.android.material.button.MaterialButton
        android:id="@+id/backBtn"
        android:layout_width="0dp"
        android:layout_height="59dp"
        android:text="Back"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        app:backgroundTint="#470000"
        app:cornerRadius="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

Upvotes: 1

user14905573
user14905573

Reputation:

Try this XML code

 <com.google.android.material.button.MaterialButton
        android:id="@+id/backBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/back_btn_bg"
        android:fontFamily="@font/montserrat_thin"
        android:text="@string/back_btn"
        app:backgroundTint="#470000"
        app:cornerRadius="0dp"
        app:fontFamily="@font/montserrat_thin"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

Upvotes: 0

Related Questions