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.

// Simple click listener for a button
button.onClick { clickedView ->
    // Do something when the button is clicked
    Toast.makeText(context, "Button clicked!", Toast.LENGTH_SHORT).show()
}

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 a Boolean value. This allows you to control whether the long click event should be consumed (return true) or allow further processing (return false).

// Long click listener for an image view that consumes the event
imageView.onLongClick { clickedView ->
    // Do something on long click
    Log.d("LongClick", "Image long-clicked!")
    true // Consume the event
}

// Long click listener for a text view that allows further processing
textView.onLongClick { clickedView ->
    // Show a context menu on long click
    showContextMenu(clickedView)
    false // Don't consume the event
}

Visibility Function:

  • visible(): Sets the view's visibility to View.VISIBLE.

  • gone(): Sets the view's visibility to View.GONE (completely removes the view from the layout hierarchy).

  • invisible(): Sets the view's visibility to View.INVISIBLE (view still occupies space but is not drawn).

// visible
textView.visible()

// gone
textView.gone()

// invisible
textView.invisible()

Conditional Visibility Functions:

  • onlyGoneIf(condition: () -> Boolean): Hides the view (sets visibility to GONE) if the provided condition function returns true, otherwise makes it visible.

  • onlyVisibleIf(condition: () -> Boolean): Shows the view (sets visibility to VISIBLE) if the provided condition function returns true, otherwise hides it.

  • onlyInvisibleIf(condition: () -> Boolean): Makes the view invisible (sets visibility to INVISIBLE) if the provided condition function returns true, otherwise shows it.

  • visibleIf(condition: () -> Boolean): Shows the view (sets visibility to VISIBLE) if the provided condition function returns true and the view's current visibility is not already FOCUSABLES_ALL. This additional check ensures the view is properly focusable if needed.

// gone a image view if the url is null
imageView.onlyGoneIf { url == null }

// visible a image view if the url not null
imageView.onlyVisibleIf { url != null }

// invisible a image view if the url is null
imageView.onlyInvisibleIf { url == null }

// visible a image view if the url not null
imageView.visibleIf { url != null }

Enable/Disable Functions:

  • enable(): Enables the view (sets isEnabled to true).

  • disable(): Disables the view (sets isEnabled to false).

// enable a button
button.enable()

// disable button
button.disable()

Conditional Enable/Disable Functions:

  • disableIf(condition: () -> Boolean): Disables the view if the provided condition function returns true, otherwise enables it.

  • enableIf(condition: () -> Boolean): Enables the view if the provided condition function returns true, otherwise disables it.

// Enable a button if a condition is met
button.enableIf { someVariable == true }

// Disable a button if a condition is met
button.disableIf { someVariable != true }

CompoundButton Functions:

  • check(): Checks the CompoundButton (e.g., CheckBox, RadioButton).

  • uncheck(): Unchecks the CompoundButton.

// Check a checkbox based on a condition
checkBox.check()

// Check a checkbox based on a condition
checkBox.uncheck()

EditText/TextInputLayout Text Change:

  • EditText.onTextChange(doOnChange: (String) -> Unit): Attaches a listener to the EditText that calls the provided doOnChange 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 on TextInputLayout and uses the associated EditText within it.

// Listen for text changes in an EditText
editText.onTextChange { newText ->
    // Do something with the new text
}

// Listen for text changes in an TextInputLayout
textInputLayout.onTextChange { newText ->
    // Do something with the new text
}

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