0

Disclaimer: I already read the post at onClick event in navigation drawer, but sadly it does not work for me.

I am pretty new to Android I am just making my first app in Kotlin and I wanted to integrate an NavigationDrawer. In it I have integrated an option "Website" which should just launch a website. Just when I click on it it closes the drawer, but does not start the Intent for the website.

My Code: (I removed unimportant parts that are needed for the rest of the app)

MainActivity.kt:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

private lateinit var drawerLayout: DrawerLayout
private lateinit var aToggle: ActionBarDrawerToggle

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(activity_main)
    createNotificationChannel()
    setNavigationViewListener()

    val toolbar: Toolbar = toolbar
    setSupportActionBar(toolbar)
    val actionbar: ActionBar? = supportActionBar
    actionbar?.apply {
        setDisplayHomeAsUpEnabled(true)
        setHomeAsUpIndicator(R.drawable.ic_menu)
    }

    drawerLayout = drawer_layout
    aToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)
    drawerLayout.addDrawerListener(aToggle)

    drawerLayout.addDrawerListener(
            object : DrawerLayout.DrawerListener {
                override fun onDrawerSlide(drawerView: View, slideOffset: Float) {

                }

                override fun onDrawerOpened(drawerView: View) {

                }

                override fun onDrawerClosed(drawerView: View) {

                }

                override fun onDrawerStateChanged(newState: Int) {

                }
            }
    )

    aToggle.syncState()
    val navigationView: NavigationView = findViewById(R.id.nav_view)
    navigationView.setNavigationItemSelectedListener { menuItem ->
        // set item as selected to persist highlight
        menuItem.isChecked = true
        // close drawer when item is tapped
        drawerLayout.closeDrawers()

        // Add code here to update the UI based on the item selected
        // For example, swap UI fragments here

        true
    }
}

private fun setNavigationViewListener() {
    val navigationView = nav_view
    navigationView.setNavigationItemSelectedListener(this)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.drawer_view, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        android.R.id.home -> {
            drawerLayout.openDrawer(GravityCompat.START)
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
    if(aToggle.onOptionsItemSelected(item)) {
        return true
    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.website -> {
            val i = Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://gym-wen.de/vp/"))
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
            return true
        }
        else -> {}
    }
    return true
}
}

activity_main.xml:

<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.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:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <ListView
        android:id="@+id/vertretungs_list"
        android:paddingTop="?attr/actionBarSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/drawer_view" />

drawer_view.xml (menu):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/website"
        android:title="@string/website" />
    <item
        android:id="@+id/nav_gallery"
        android:title="2" />
    <item
        android:id="@+id/nav_slideshow"
        android:title="3" />
    <item
        android:id="@+id/nav_manage"
        android:title="4" />
</group>

I am pretty sure I forgot something, but I just do not know what it is. I read many tutorials and other posts, but I just do not know it. Hopefully someone can help me :)

Greetings

KITRIK
  • 37
  • 1
  • 8

2 Answers2

1

You have set two listeners for the same things. One overwrites the other.

navigationView.setNavigationItemSelectedListener { menuItem ->
    // set item as selected to persist highlight
    menuItem.isChecked = true
    // close drawer when item is tapped
    drawerLayout.closeDrawers()

    // Add code here to update the UI based on the item selected
    // For example, swap UI fragments here

    true
}

Which does not launch the website and navigationView.setNavigationItemSelectedListener(this) which should.

I were you I would remove the entire first block and implement everything in override fun onOptionsItemSelected(item: MenuItem): Boolean

shkschneider
  • 17,833
  • 13
  • 59
  • 112
  • You are a hero! It works perfectly fine now I just forgot about that part. My Code got pretty messy :D – KITRIK Jan 04 '19 at 15:15
0

Your activity class implements NavigationView.OnNavigationItemSelectedListener,
so instead of setting as a listener:

navigationView.setNavigationItemSelectedListener { menuItem ->
    // set item as selected to persist highlight
    menuItem.isChecked = true
    // close drawer when item is tapped
    drawerLayout.closeDrawers()

    // Add code here to update the UI based on the item selected
    // For example, swap UI fragments here

    true
}

you should call:

navigationView.setNavigationItemSelectedListener(this)

so this method is called:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.website -> {
            val i = Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://gym-wen.de/vp/"))
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
            return true
        }
        else -> {}
    }
    return true
}

and in this method you manipulate the closing of the drawer or the checked items.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • Thank you very much! It works perfektly now. Thanks! – KITRIK Jan 04 '19 at 15:16
  • @ChuckNorris I think you tried to accept both answers, but you can't. No worries as both answers are the same and posted at the same time. Just to know you can accept only 1 answer and when you have rep at least 15 you can also upvote other answers. – forpas Jan 04 '19 at 15:22
  • thanks for making that clear :) sadly I only have 8 reputation :/ – KITRIK Jan 04 '19 at 18:38
  • I said no worries – forpas Jan 04 '19 at 18:47