Skip to content

Instantly share code, notes, and snippets.

@miguelhincapie
Created April 5, 2020 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelhincapie/ea2a9b0079148ba55882ff363f3e89e7 to your computer and use it in GitHub Desktop.
Save miguelhincapie/ea2a9b0079148ba55882ff363f3e89e7 to your computer and use it in GitHub Desktop.
Extension functions to deal with enable/disable buttons with accessibility service ON.
/**
* Extension function to disable a Button.
* Workaround due we can't just set enable to false because it will lose focusable ability.
* Check canTakeFocus() function inside View.java file.
*/
fun Button.disable(@DrawableRes drawableForAccessibility: Int) {
if (TalkBackState.value == true) {
isEnabled = true
isClickable = false
contentDescription = String.format(context.getString(R.string.button_disabled_accessibility), text)
background = ContextCompat.getDrawable(context, drawableForAccessibility)
} else {
isEnabled = false
}
}
/**
* Extension function to enable a Button.
* Workaround due we can't just set enable to false because it will lose focusable ability.
* Check canTakeFocus() function inside View.java file.
*/
fun Button.enable(@DrawableRes drawableForAccessibility: Int) {
if (TalkBackState.value == true) {
isEnabled = true
isClickable = true
contentDescription = context.getString(R.string.button_accessibility_label, text)
background = ContextCompat.getDrawable(context, drawableForAccessibility)
} else {
isEnabled = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment