Marco Ripamonti
Marco Ripamonti

Reputation: 99

Add toolbar in constraintLayout of a fragment layout

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?

Upvotes: 0

Views: 4108

Answers (3)

Marco Ripamonti
Marco Ripamonti

Reputation: 99

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

Upvotes: 0

PushpikaWan
PushpikaWan

Reputation: 2545

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.

Upvotes: 1

Ahmed Wahdan
Ahmed Wahdan

Reputation: 352

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>

Upvotes: 1

Related Questions