2

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>
2
  • Post your layout Commented Jun 17, 2020 at 19:58
  • @GabrieleMariotti Done Commented Jun 17, 2020 at 23:20

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

8 Comments

it works but when i removed the layout_gravity it covers the main activity components
@AhmadAl-Maghrabi In which view did you remove the layout_gravity?
i removed layout_gravity from navigation view i didn't use it in Drawer layout view
@AhmadAl-Maghrabi sorry my bad. The answer wan't clear. Don't remove android:layout_gravity from the NavigationView (check the updated answer) but from the first child (main content).
it wasn't exist in the main content
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.