0

I'm trying to add a toolbar inside a constraint layout but this is the result I'm getting:

enter image description here

As you can see it doesn't fit the whole screen. Here's my xml:

<android.support.constraint.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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment title"/>

    </android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>

The ConstraintLayout width and length are set to match parent (which should be the screen?) and the toolbar width is set to match the layout width. What am I missing?

Marco Ripamonti
  • 77
  • 2
  • 12

3 Answers3

1

Try this out by adding these content inside toolbar

android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp" 

I got this from Android: remove left margin from actionbar's custom layout. And u can try other solutions in there as well if this is not solve your problem.

PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
1

try this you have to set the constraintTop to the parent and also make width value match constraints and set start and end constraint to parent,

check the code below

<android.support.constraint.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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment title" />

    </android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>
Ahmed Wahdan
  • 330
  • 4
  • 15
0

I solved this issue by putting the action bar on the main activity instead of the fragment layout.

Marco Ripamonti
  • 77
  • 2
  • 12