Search code examples
javaandroidxmldrawerlayout

Drawer Menu Not Displaying in Android Activity Welcome Page


I am currently facing an issue with the display of the main menu in my Android activity, specifically in the activity_welcome.xml layout file.

After a user logs in, they are directed to the activity_welcome page, which should display a welcome message followed by a menu of options. The menu is implemented using a DrawerLayout with a NavigationView.

The issue: The welcome message (within a RelativeLayout) displays correctly, but the menu does not appear on the screen.

Below are the relevant XML layout files:

activity_welcome.xml: This layout file contains the DrawerLayout and the NavigationView for the menu.

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:visibility="visible">

    <!-- Content View -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"><!---->

        <!-- Large Welcome Text -->
        <TextView
            android:id="@+id/welcomeText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="32dp"
            android:gravity="center"
            android:text="Welcome [User Name]"
            android:textSize="24sp"
            android:textStyle="bold" /><!---->

        <!-- User Information -->
        <TextView
            android:id="@+id/userNameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/welcomeText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="User Name: "
            android:textSize="18sp" />

        <TextView
            android:id="@+id/userEmailText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/userNameText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="Email: "
            android:textSize="18sp" />

        <TextView
            android:id="@+id/userIdText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/userEmailText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="User ID: "
            android:textSize="18sp" /><!---->


    </RelativeLayout><!---->
    <!-- Navigation Drawer -->
    <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"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu"
         />
</androidx.drawerlayout.widget.DrawerLayout>

nav_header.xml: This is the layout for the header of the navigation view.

<?xml version="1.0" encoding="utf-8"?><!-- nav_header.xml -->

<RelativeLayout 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="160dp"
    >

    <!-- Your app's logo or an icon -->
    <ImageView
        android:id="@+id/nav_header_icon"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:src="@mipmap/ic_launcher" />

    <!-- A title or app name -->
    <TextView
        android:id="@+id/nav_header_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nav_header_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:text="SongTracker"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold" />
</RelativeLayout>

drawer_menu.xml: This file defines the menu items in the navigation view.

<?xml version="1.0" encoding="utf-8"?><!-- nav_header.xml -->

<RelativeLayout 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="160dp"
    >

    <!-- Your app's logo or an icon -->
    <ImageView
        android:id="@+id/nav_header_icon"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:src="@mipmap/ic_launcher" />

    <!-- A title or app name -->
    <TextView
        android:id="@+id/nav_header_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nav_header_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:text="SongTracker"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold" />
</RelativeLayout>

And just to cover all bases - here is my themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SongTracker" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
    <style name="BackgroundLightGray" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/light_gray</item>
    </style>
    <style name="OrangeTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/orange</item>
    </style>
    <style name="AppTheme.NavigationView" parent="Base.Theme.AppCompat.Light">
        <item name="android:background">@color/black</item>
        <item name="android:textColor">@color/white</item>
    </style>


</resources>

Troubleshooting Steps Taken:

  • I have ensured that the visibility attributes (android:visibility) for both the DrawerLayout and the RelativeLayout are set to "visible".

  • I have defined a custom theme (AppTheme.NavigationView) for the
    NavigationView to specify text and background colors (see themes.xml above).

  • reviewed my layout hierarchy using Android Studio's Layout
    Inspector and didn't find any overlapping views or issues.

  • tested the app on different emulators and devices to rule out device-specific problems.

  • reviewed my Java code for any programmatic conditions
    affecting the visibility of the NavigationView.

Request for Assistance:

I kindly request assistance in identifying and resolving the issue causing the main menu not to display in the activity_welcome layout. Any insights, suggestions, or troubleshooting steps would be greatly appreciated.

Thank you in advance for your help!


Solution

  • First menu xml files is different from layout files and you just have to change it and make it like this i mean drawer_menu.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/your_id"
            android:title="Title"
            android:icon="@mipmap/ic_launcher"  
            android:showAsAction="always" />
    
    </menu>
    

    and add openDrawer attribute in the drawer layout:

    <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:id="@+id/drawer_layout"
    tools:openDrawer="start">