yousef ali
yousef ali

Reputation: 35

Set onClickListener For Whole Screen

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

Answers (5)

Code-Hunter
Code-Hunter

Reputation: 1

To set the click listener to whole activity/screen

findViewById<View>(android.R.id.content).setOnClickListener{
    // do something here

}

Upvotes: 0

Amin Mousavi
Amin Mousavi

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

End User
End User

Reputation: 812

Give id to your constraint layout and do something like this

constraintlayoutId.setOnClickListener{

}

Upvotes: 1

OhhhThatVarun
OhhhThatVarun

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

ZeroOneZeroR
ZeroOneZeroR

Reputation: 703

Set click listener to whole screen

findViewById<View>(android.R.id.content).rootView.setOnClickListener{
        // do something here
    }

Upvotes: 2

Related Questions