Ahmad Al-Maghrabi
Ahmad Al-Maghrabi

Reputation: 35

Navigation View Menu Item Doesn't respond to any Click

i want to know what is wrong with my code as there is no any responding to my clicks on the menu item

class Main : AppCompatActivity() , NavigationView.OnNavigationItemSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        setSupportActionBar(mainToolbar)

        supportActionBar?.title = ""
        val toolbarToggle = ActionBarDrawerToggle(this,DrawerLayout,mainToolbar,R.string.drawer_open,R.string.drawer_close)
        DrawerLayout.addDrawerListener(toolbarToggle)
        toolbarToggle.syncState()

        mainNavigView.setNavigationItemSelectedListener(this)

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        var titleT = item.title
        Toast.makeText(this,titleT, Toast.LENGTH_LONG).show()
        return true
    }

}

this is xml code for navigation and drawer


<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/DrawerLayout"
    android:background="@color/spark_bg"
    tools:context=".Main">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/mainNavigView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigationheader"
        app:menu="@menu/navigationmenu"
         />

this is menu item code that contains id for the item i wanted to press

<menu xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@+id/myOrdersMI"
                android:icon="@drawable/ic_list"
                android:title="My Orders"
                />

</menu>

Upvotes: 2

Views: 693

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363439

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no <layout_gravity>

    <androidx.drawerlayout.widget.DrawerLayout
       ...>

        <!-- main content goes here -->

        <!-- NavigationView -->
        <com.google.android.material.navigation.NavigationView
            android:layout_gravity="start|left"
            ../>

    </androidx.drawerlayout.widget.DrawerLayout>

Upvotes: 2

Related Questions