Inside DrawerLayout, I cannot put anything like toolbar or constraintLayout

32 views Asked by At

This is my code for main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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">

    

    <com.google.android.material.navigation.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/menu_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

When I try to add <android...toolbar> here it doesn't prompt any suggestions, including layout_width/height. When I manually fill them in, nothing shows up on the screen. Same with TextView.

I tried restarting the IDE and re-syncing the gradle but nothing.

1

There are 1 answers

0
Sarah On

Why don't you try it this way? I'll give you my code related to this. It worked for me.

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/Theme.RealNoteApp.ToolbarFont"
        bind:isVisible="@{!notesViewModel.isLoading &amp;&amp; !notesViewModel.hasError}" />

...

</LinearLayout>
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_menu" />
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>