You can custom a RadioButton class like:
class CustomRadioButton : AppCompatRadioButton {
var talkBackString: CharSequence? = null
// override to disable the "radiobutton" readout
override fun getAccessibilityClassName(): CharSequence {
return CustomRadioButton::class.java.simpleName
}
/**
* [CustomRadioButton] default constructor
* @param context is an activity [Context]
* @param attrs is an [AttributeSet]
* @param defStyle is [Int] value of style
*/
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
context,
attrs,
defStyle
) {
setupView()
}
/**
* [CustomRadioButton] default constructor
* @param context is an activity [Context]
* @param attrs is an [AttributeSet]
*/
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
setupView()
}
/**
* [CumtomRadioButton] default constructor
* @param context is an activity [Context]
*/
constructor(context: Context) : super(context) {
setupView()
}
/**
* This method is used to setup view's attributes
*/
private fun setupView() {
accessibilityDelegate = MyAccessibilityDelegate()
}
/**
* Custom class AccessibilityDelegate
*/
inner class MyAccessibilityDelegate : AccessibilityDelegate() {
/**
* Override function onInitializeAccessibilityNodeInfo of AccessibilityDelegate
* @param host is [View]
* @param info is [AccessibilityNodeInfo]
*/
override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfo) {
super.onInitializeAccessibilityNodeInfo(host, info)
if (talkBackString.isNullOrEmpty() && host.contentDescription.isNullOrEmpty().not()) {
talkBackString = host.contentDescription
}
info.apply {
isCheckable = false
// ...and then you can set whatever you want as a text
//format: Radio button + desciption + state of radio button
text = "Radio button" + "\n" + talkBackString.toString() + "\n" + getDescription()
}
}
}
override fun setChecked(checked: Boolean) {
if (checked == isChecked) return
super.setChecked(checked)
// since we've disabled the checked/unchecked readouts
// we are forced to manually announce changes to the state
announceForAccessibility(getDescription())
}
private fun getDescription(): String {
return if (isChecked) {
"Selected"
} else {
"Not selected"
}
}
}
Layout:
<com.example.app.CustomRadioButton
android:id="@+id/itemRadioButton"
android:layout_width="@dimen/_30dp"
android:layout_height="@dimen/_30dp"
android:layout_marginVertical="@dimen/_10dp"
android:layout_marginStart="@dimen/_20dp"
android:focusable="false" />
Using:
itemRadiobutton.talkBackString = "Alo alo"
Then talkback will read aloud: RadioButton + Alo alo + selected/not selected