View
These code snippets define various extension functions for the View
class in Android development using Kotlin. Extension functions allow you to add functionality to existing classes without modifying the original class itself.
Onclick Function
onClick(action: (View) -> Unit):
This function allows you to easily attach a click listener to any
View
object.It takes a single argument, which is a lambda function with a single parameter of type
View
.This lambda function represents the action you want to perform when the view is clicked. The
View
parameter passed to the lambda is the view that was clicked.
onLongClick(action: (View) -> Boolean):
This function attaches a long click listener to a
View
object.It takes a single argument, which is a lambda function with a single parameter of type
View
.This lambda function represents the action you want to perform when the view is long-clicked. The
View
parameter passed to the lambda is the view that was long-clicked.Unlike
onClick
,onLongClick
returns aBoolean
value. This allows you to control whether the long click event should be consumed (returntrue
) or allow further processing (returnfalse
).
Visibility Function:
visible()
: Sets the view's visibility toView.VISIBLE
.gone()
: Sets the view's visibility toView.GONE
(completely removes the view from the layout hierarchy).invisible()
: Sets the view's visibility toView.INVISIBLE
(view still occupies space but is not drawn).
Conditional Visibility Functions:
onlyGoneIf(condition: () -> Boolean)
: Hides the view (sets visibility toGONE
) if the providedcondition
function returnstrue
, otherwise makes it visible.onlyVisibleIf(condition: () -> Boolean)
: Shows the view (sets visibility toVISIBLE
) if the providedcondition
function returnstrue
, otherwise hides it.onlyInvisibleIf(condition: () -> Boolean)
: Makes the view invisible (sets visibility toINVISIBLE
) if the providedcondition
function returnstrue
, otherwise shows it.visibleIf(condition: () -> Boolean)
: Shows the view (sets visibility toVISIBLE
) if the providedcondition
function returnstrue
and the view's current visibility is not alreadyFOCUSABLES_ALL
. This additional check ensures the view is properly focusable if needed.
Enable/Disable Functions:
enable()
: Enables the view (setsisEnabled
totrue
).disable()
: Disables the view (setsisEnabled
tofalse
).
Conditional Enable/Disable Functions:
disableIf(condition: () -> Boolean)
: Disables the view if the providedcondition
function returnstrue
, otherwise enables it.enableIf(condition: () -> Boolean)
: Enables the view if the providedcondition
function returnstrue
, otherwise disables it.
CompoundButton Functions:
check()
: Checks theCompoundButton
(e.g., CheckBox, RadioButton).uncheck()
: Unchecks theCompoundButton
.
EditText/TextInputLayout Text Change:
EditText.onTextChange(doOnChange: (String) -> Unit)
: Attaches a listener to theEditText
that calls the provideddoOnChange
function whenever the text changes. The function receives the new text as a string.TextInputLayout.onTextChange(doOnChange: (String) -> Unit)
: Similar to the above, but works onTextInputLayout
and uses the associatedEditText
within it.
These extension functions provide a concise and easy-to-read way to manage event listener, view visibility, enable/disable state, text changes, and CompoundButton
interactions in your Android development using Kotlin.
Last updated