Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout is not matching parent width inside a ToolBar

So I have a customized toolbar on my home screen, and within this toolbar is a RelativeLayout. I have the layout width set to match_parent and the parent of the relative layout is just the width of the screen.

For some reason, it does not fit the width of the screen because the left edge is some distance away from the edge of the screen. I am not sure why this is but it is making positioning within the RelativeLayout a bit more difficult.

This is my .xml

<android.support.v7.widget.Toolbar
    android:id="@+id/products_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/bg_gradient"
    app:layout_constraintStart_toStartOf="parent"
    app:titleTextColor="#000000">

    <RelativeLayout
        android:id="@+id/topRL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="16dp">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="72sp"
            android:layout_height="37sp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="137dp"
            android:layout_marginTop="11dp"
            android:layout_marginEnd="159dp"
            android:fontFamily="@font/veganstyle"
            android:gravity="center"
            android:text="BL"
            android:textAlignment="center"
            android:textColor="#efefef"
            android:textSize="20dp"
            android:textStyle="bold" />

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

And this is a screenshot showing what I am talking about.

enter image description here

As you can see, the blue box surrounding the RelativeLayout is not at the edge of the screen, nor will it let me bring it to the edge.

Neither the RelativeLayout or its parent have any padding or margins, so I am confused why this is an issue and how I can solve it.

like image 638
brent_mb Avatar asked Jan 28 '26 10:01

brent_mb


1 Answers

You need to set the following attribute in your ToolBar

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

Hence your layout should be something like the following.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/products_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:titleTextColor="#000000">

    <RelativeLayout
        android:id="@+id/topRL"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="72sp"
            android:layout_height="37sp"
            android:layout_centerInParent="true"
            android:text="BL"
            android:textAlignment="center"
            android:textColor="#efefef"
            android:textSize="20dp"
            android:textStyle="bold" />

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

Here is this from the developer's documentation.

like image 107
Reaz Murshed Avatar answered Jan 31 '26 21:01

Reaz Murshed