Search code examples
androidnavigation-drawerandroidx

Hamburger icon on Toolbar acts as back button


I have tried to implement navigation drawer in my app with androidx library. So far, the app show hamburger icon on top left corner, but to access the drawer I have to swipe right. If I clicked the icon it will go to previous activity instead. Is it because this activity is not the main activity? How can I fix this? Thank you in advance.

Here is my activity

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
        ActionBarDrawerToggle t = new ActionBarDrawerToggle(this, drawerLayout,R.string.Open, R.string.Close);
        drawerLayout.addDrawerListener(t);
        t.syncState();

        NavigationView nv = findViewById(R.id.navigationView);
        nv.setNavigationItemSelectedListener(item -> {
            int id = item.getItemId();
            switch(id)
            {
                case R.id.action_open_list:
                    break;
                case R.id.action_closed_list:
                    Intent closedListIntent = new Intent(this, ClosedListActivity.class);
                    startActivity(closedListIntent);
                    break;
                default:
                    return true;
            }


            return true;

        });
    }

my layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_error_message_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="@string/error_message_cannot_connect"
            android:textSize="20sp"
            android:visibility="invisible" />

        <ProgressBar
            android:id="@+id/pb_loading_indicator"
            android:layout_width="42dp"
            android:layout_height="42dp"
            android:layout_gravity="center"
            android:visibility="invisible" />

    </FrameLayout>
    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        android:id="@+id/navigationView"/>

</androidx.drawerlayout.widget.DrawerLayout>

Solution

  • Toolbar needs to be a child of DrawerLayout to work with.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawerLayout"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <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="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
    
                <TextView
                    android:id="@+id/tv_error_message_display"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:text="@string/error_message_cannot_connect"
                    android:textSize="20sp"
                    android:visibility="invisible" />
    
                <ProgressBar
                    android:id="@+id/pb_loading_indicator"
                    android:layout_width="42dp"
                    android:layout_height="42dp"
                    android:layout_gravity="center"
                    android:visibility="invisible" />
    
            </FrameLayout>
        </LinearLayout>
    
        <com.google.android.material.navigation.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:menu="@menu/navigation_menu"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            android:id="@+id/navigationView"/>
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    Then in your Activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ....
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id. drawerLayout);
        ActionBarDrawerToggle t = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.Open, R.string.Close);
        drawerLayout.addDrawerListener(t);
        t.syncState();
    
        ....
    }