I tried to add drawer to one of the activities in my app. However the Navigation View covers all other layouts and it's the only thing I can see when I open the Activity.I can not slide it back,or do anything with it. Other objects seem to be placed behind that drawer somehow. Sorry if it's something simple, it's my first time doing it. Does anybody know why that may happen?
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".CategoryActivity"
android:id="@ id/drawer_layout"
android:fitsSystemWindows="true"
android:background="@color/dark_blue"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
android:foregroundGravity="right"
>
</com.google.android.material.navigation.NavigationView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/dark_blue">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="@ id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:textColor="#ffffff"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginStart="130dp"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="@drawable/hist_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="40dp"
android:gravity="end"
android:text="History"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@ id/bt_hist_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="270dp"
android:layout_marginTop="100dp"
android:background="@drawable/his_btn" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="@drawable/android_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:gravity="start"
android:text="Programming"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginStart="20dp"/>
<Button
android:id="@ id/bt_prog_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="100dp"
android:background="@drawable/an_btn" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:src="@drawable/allknow_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="40dp"
android:gravity="end"
android:text="Technology"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@ id/bt_tech_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="270dp"
android:layout_marginTop="100dp"
android:background="@drawable/allknow_btn" />
</RelativeLayout>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Here is the Activity code:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
CodePudding user response:
I can see few missing things in your xml code.
- There is no parent
<AppBarLayout
tag for your<Toolbar
tag - What is the purpose of NavigationView? You haven't given it any ID and not using it?
- The NavigationView
height
should bematch_parent
- Remove
foregroundGravity
in NavigationView and addandroid:layout_gravity="start"
, also addandroid:fitsSystemWindows="true"
You should initialize your NavigationView
in your Main Activity
and use it to setup NavController
. I am not giving you the code for everything so you can search a little bit yourself but if you don't find the solutions let me know.