Reputation: 35
Hello Iam Fairly New To Kotlin
I Heard That You Can Set onClickListener for the whole screen in java.
so I was asking
1-Can You Do the same thing in kotlin?
2-How to set onClickListener for constraint-layout in kotlin?
and thanks in advance
Upvotes: -1
Views: 887
Reputation: 1
To set the click listener to whole activity/screen
findViewById<View>(android.R.id.content).setOnClickListener{
// do something here
}
Upvotes: 0
Reputation: 1290
To answer your questions:
Can You Do the same thing in Kotlin?
Yes you can since Kotlin can use all the Android APIs which were written in Java so there won't be any problem
How to set onClickListener for constraint-layout in Kotlin?
Give your `ConstrainyLayout` and ID and then use `findViewById`, `ViewBinding`, or ... the get a reference of it inside your Activity/Fragment/View and call `setOnClickListener` on it and do anything you want inside of it
Upvotes: 1
Reputation: 812
Give id to your constraint layout
and do something like this
constraintlayoutId.setOnClickListener{
}
Upvotes: 1
Reputation: 4330
So, ConstraintLayout
is just another View
and so if you want to set a click listener to it you just need its reference and call the method onClickListener
on it.
findViewById<View>(R.id.yourConstraintLayoutId).setOnClickListener {
// Do something here
}
Upvotes: 0
Reputation: 703
Set click listener to whole screen
findViewById<View>(android.R.id.content).rootView.setOnClickListener{
// do something here
}
Upvotes: 2