0

I have RecyclerView as the page's content, then I added drawer navigation. The drawer navigation works just fine. But the page's content got move up a little, so that leaves some text covered by ActionBar. I tried using layout_marginTop to move it down a little, and it worked. But I'm not sure what's causing everything to ignore ActionBar's position. As you can see, some of the text is covered by ActionBar

Here's my XML file:

<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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NothingActivity"
    tools:openDrawer="start">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:scrollbars="vertical" />
    <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="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/Theme.AppCompat.Light"
            android:elevation="4dp" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


    </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/drawer_menu"/>


</androidx.drawerlayout.widget.DrawerLayout>
Robotic Reaper
  • 417
  • 1
  • 4
  • 15

2 Answers2

1

you can try a small hack. You can add marginTop to your recyclerView which is equal to the height of your ActionBar (by default it's 56dp). This will shift your recyclerView below the ActionBar. You can add some extra margin if you want.

Hope it helps.

Akanshi Srivastava
  • 1,160
  • 13
  • 24
0

Change the order of Recyclerview and Toolbar, moreover delete fragment_container

<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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NothingActivity"
tools:openDrawer="start">

<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="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/Theme.AppCompat.Light"
        android:elevation="4dp" />



</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:scrollbars="vertical" />

<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/drawer_menu"/>

Ahmet B.
  • 1,290
  • 10
  • 20
  • Then it crashes and say `java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference`. But this error only happens after I changed the layout to yours. – Robotic Reaper Jun 15 '20 at 15:29