0

I have an Android app and my app has 4 Activities. I am using CardView for switching the Activity. I want it so that when the user clicks the CardView the user will go to the targeted Activity.

The problem is I am using #Kotlin and I can't set CardView#OnClickListener using Kotlin. How can I set the OnClickListener using Kotlin?

David Rawson
  • 20,912
  • 7
  • 88
  • 124
Arman Hossain
  • 21
  • 2
  • 7

2 Answers2

2

You can do this

cardView.setOnClickListener{
        //Access view by using `it`
    }
Dishonered
  • 8,449
  • 9
  • 37
  • 50
1

Initialize this in onCreate of the activity.

CardViewId.setOnClickListener(clickListener)

Add this code snippet as a method anywhere in your activity:

   private val clickListener: View.OnClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.CardViewId -> gotoXScreent()
        }
    }

A method to change activity where you can pass your activity:

  private fun goToXScreen() {
        val intent = Intent(this, ABCActivity::class.java)
        startActivity(intent)
    }
David Rawson
  • 20,912
  • 7
  • 88
  • 124
Jyot
  • 540
  • 5
  • 17