I want to know how to open drawerlayout from left by sliding anywhere in view of the layout.
I tried this: https://stackoverflow.com/a/71389377/21290837, but it didn't let me to initialize my drawerlayout in the end.
EDIT
XML:
<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/dLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<include
layout="@layout/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</include>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header_main"
app:menu="@menu/menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity:
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.navView.setNavigationItemSelectedListener(this)
...
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.fish ->
adapter?.updateAdapter(fillArrays(resources.getStringArray(R.array.fish_name),
(resources.getStringArray(R.array.content)),
getImageId(R.array.fish_image_array)))
R.id.location ->
adapter?.updateAdapter(fillArrays(resources.getStringArray(R.array.location_name),
(resources.getStringArray(R.array.location_content)),
getImageId(R.array.location_image_array)))
}
binding.dLayout.closeDrawer(GravityCompat.START)
return true
}
...
}