I am trying to make an image view click open navigation drawer. This is the code i have in main activity
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context="zw.zse.direct.activities.client.HomeActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:visibility="gone"
android:id="@+id/nav_view"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/menu_nav_drawer"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/colorPrimary" />
<include layout="@layout/top_header"
android:id="@+id/top_header"/>
<WebView
android:layout_below="@+id/top_header"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_below="@+id/webview"
android:id="@+id/fragment_placeHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_alignParentBottom="true"
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="labeled"
style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"
app:menu="@menu/bottom_navigation_menu" />
</RelativeLayout>
<!--bottom sheet container -->
<FrameLayout
android:layout_alignParentBottom="true"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/bottom_sheet_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>
This is the code for the top_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Top Header Layout-->
<LinearLayout
android:background="@color/colorPrimary"
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:padding="10dp">
<ImageView
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:id="@+id/back"
android:layout_width="45dp"
android:layout_height="45dp"
android:padding="10dp"
android:src="@drawable/ic_baseline_arrow_back_24" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/icon"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp">
<ImageView
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:id="@+id/history"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:padding="6dp"
android:src="@drawable/icon_history" />
</RelativeLayout>
</LinearLayout>
I want to make it that when the imageview(id = back) is clicked, navigation drawer opens, nd can be toggled.
This is the current java code i have. The theme i am using has no action bar.
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
menuIcon = findViewById(R.id.back);
menuIcon.setImageDrawable(getResources().getDrawable(R.drawable.icon_menu));
menuIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open navigation drawer
drawerLayout.openDrawer(Gravity.LEFT);
}
});
At the moment with this code the activity starts with the drawer open and i cannot close it