Search code examples
androidlistviewnavigation-drawer

Listview not appearing when using DrawerLayout


I'm trying to make an activity that has a listview and a navigation drawer. The listview was working fine before I implemented the drawerlayout. How to fix this?

TestingActivity.xml

    <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"
    android:background="#00008577"
    tools:context=".TestingActivity"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/header"
        app:menu="@menu/main_menu"
        android:layout_gravity="start"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/purple_500"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <ListView
            android:id="@+id/lv_SubjectList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:divider="#d1d1d1"
            android:dividerHeight="5dp"
            android:layout_alignBottom="@id/toolbar"/>
    </RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>

Solution

  • <ListView
            android:id="@+id/lv_SubjectList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:layout_alignBottom="@id/toolbar"   <----- Remove this line
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:divider="#d1d1d1"
            android:dividerHeight="5dp" />