Meric Fournier
Meric Fournier

Reputation: 37

Make clickable link in menu with kotlin

I try to make a Bottom navigation, I have this code

val bottomNavigation = findViewById<View>(R.id.bottom_navigation) as 
  BottomNavigationView
    bottomNavigation.setOnNavigationItemSelectedListener { item ->
        when (item.itemId) {

            R.id.botom__nav__home ->
                // Action when tab 1 selected
                val intent = Intent(this, HomeActivity::class.java)
            R.id.botom__nav__profile ->
                // Action when tab 2 selected
                val intent = Intent(this, LikeActivity::class.java)
            else ->
                // Action when tab 3 selected
                val intent = Intent(this, ProfileActivity::class.java)
        }
        true
    }
    startActivityForResult(intent, 99)
}

I have these errors:

' Expecting an expression '
' Expecting "->" '

For each element in the " When " ...

Can somebody help me to fix these errors?

Upvotes: 0

Views: 165

Answers (2)

Dr4ke the b4dass
Dr4ke the b4dass

Reputation: 1204

Please remove the item -> in this line

bottomNavigation.setOnNavigationItemSelectedListener { item ->
    when (item.itemId) {` 

Upvotes: 0

donfuxx
donfuxx

Reputation: 11323

The problem is that you declare val intent various times inside the when block. To solve this just move the declaration of the intent outside of your when block, for example like this:

lateinit var intent:Intent
bottomNavigation.setOnNavigationItemSelectedListener { item ->
    when (item.itemId) {

        R.id.botom__nav__home ->
            // Action when tab 1 selected
            intent = Intent(this, HomeActivity::class.java)
        R.id.botom__nav__profile ->
            // Action when tab 2 selected
            intent = Intent(this, LikeActivity::class.java)
        else ->
            // Action when tab 3 selected
            intent = Intent(this, ProfileActivity::class.java)
    }
    true
}
startActivityForResult(intent, 99)

Upvotes: 1

Related Questions