diff --git a/README.md b/README.md index c500bb0..50a0781 100644 --- a/README.md +++ b/README.md @@ -59,17 +59,23 @@ anything you can with `AppCompatImageView`. ### Attributes -| Attribute | Description | Type | Default | -|---------------|------------------------------------|-----------|---------| -| `app:checked` | Sets the initial state of the icon | `boolean` | `false` | +| Attribute | Description | Type | Default | +|-----------------------------------|------------------------------------------------------------|-----------|---------| +| `app:checked` | Sets the initial state of the icon | `Boolean` | `false` | +| `app:checkedContentDescription` | Sets the initial checked content description of the icon | `String` | `null` | +| `app:uncheckedContentDescription` | Sets the initial unchecked content description of the icon | `String` | `null` | ### Methods -| Method | Description | Return | -|--------------------------------|-------------------------------------------------------------|-----------| -| `toggle()` | Toggles between the checked and unchecked state of the icon | `void` | -| `isChecked()` | Returns whether the icon is checked | `boolean` | -| `setChecked(checked: Boolean)` | Sets the checked state of the icon | `void` | +| Method | Description | Return | +|--------------------------------------------------------------------------------------------------|-------------------------------------------------------------|-----------| +| `toggle()` | Toggles between the checked and unchecked state of the icon | - | +| `.isChecked`
`isChecked()` | Returns the checked state of the icon | `Boolean` | +| `.isChecked = Boolean`
`setChecked(isChecked: Boolean)` | Sets the checked state of the icon | - | +| `.checkedContentDescription`
`getCheckedContentDescription()` | Returns the checked content description of the icon | `String?` | +| `.checkedContentDescription = String?`
`setCheckedContentDescription(value: String?)` | Sets the checked content description of the icon | - | +| `.uncheckedContentDescription`
`getUncheckedContentDescription()` | Returns the unchecked content description of the icon | `String?` | +| `.uncheckedContentDescription = String?`
`setUncheckedContentDescription(value: String?)` | Sets the unchecked content description of the icon | - | ### Events diff --git a/lib/src/main/java/og/android/lib/toggleiconview/ToggleIconView.kt b/lib/src/main/java/og/android/lib/toggleiconview/ToggleIconView.kt index d3ccef2..c3f1173 100755 --- a/lib/src/main/java/og/android/lib/toggleiconview/ToggleIconView.kt +++ b/lib/src/main/java/og/android/lib/toggleiconview/ToggleIconView.kt @@ -14,7 +14,11 @@ abstract class ToggleIconView @JvmOverloads constructor( private lateinit var mCheckedDrawable: AnimatedVectorDrawableCompat private lateinit var mUncheckedDrawable: AnimatedVectorDrawableCompat + private var mCheckedContentDescription: String? = null + private var mUncheckedContentDescription: String? = null + private var mIsChecked: Boolean = false + private var mOnCheckedChangeListener: ((view: ToggleIconView, isChecked: Boolean) -> Unit)? = null init { @@ -58,6 +62,14 @@ abstract class ToggleIconView @JvmOverloads constructor( mIsChecked = isChecked } + private fun setContentDescriptionByCheckState(isChecked: Boolean) { + contentDescription = if (isChecked) { + mCheckedContentDescription + } else { + mUncheckedContentDescription + } + } + private fun handleAttributes(attrs: AttributeSet? = null, defStyleAttr: Int = 0) { val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.ToggleIconView, defStyleAttr, 0) @@ -65,7 +77,15 @@ abstract class ToggleIconView @JvmOverloads constructor( // app:checked val checked = typedArray.getBoolean(R.styleable.ToggleIconView_checked, mIsChecked) + // app:checkedContentDescription + val checkedContentDescription = typedArray.getString(R.styleable.ToggleIconView_checkedContentDescription) + mCheckedContentDescription = checkedContentDescription + // app:uncheckedContentDescription + val uncheckedContentDescription = typedArray.getString(R.styleable.ToggleIconView_uncheckedContentDescription) + mUncheckedContentDescription = uncheckedContentDescription + + setContentDescriptionByCheckState(checked) handleCheckState(checked) } finally { typedArray.recycle() @@ -84,6 +104,18 @@ abstract class ToggleIconView @JvmOverloads constructor( isChecked = !isChecked } + var checkedContentDescription: String? + get() = mCheckedContentDescription + set(value) { + mCheckedContentDescription = value + } + + var uncheckedContentDescription: String? + get() = mUncheckedContentDescription + set(value) { + mUncheckedContentDescription = value + } + var isChecked: Boolean get() = mIsChecked set(isChecked) { @@ -93,6 +125,7 @@ abstract class ToggleIconView @JvmOverloads constructor( return } + setContentDescriptionByCheckState(isChecked) handleCheckState(isChecked) invokeOnCheckedChangeListener(isChecked) } diff --git a/lib/src/main/res/values/attrs.xml b/lib/src/main/res/values/attrs.xml index ddb04d2..1b972fa 100644 --- a/lib/src/main/res/values/attrs.xml +++ b/lib/src/main/res/values/attrs.xml @@ -2,5 +2,7 @@ + + \ No newline at end of file diff --git a/sample/src/main/java/og/android/sample/toggleiconview/MainActivity.kt b/sample/src/main/java/og/android/sample/toggleiconview/MainActivity.kt index 2e6f77d..2f4f018 100644 --- a/sample/src/main/java/og/android/sample/toggleiconview/MainActivity.kt +++ b/sample/src/main/java/og/android/sample/toggleiconview/MainActivity.kt @@ -54,7 +54,10 @@ class MainActivity : AppCompatActivity() { } toggleIconView.setOnCheckedChangeListener { view: ToggleIconView, _: Boolean -> - val value = "[${view::class.qualifiedName.toString()}:onCheckedChanged] isChecked: ${toggleIconView.isChecked}" + val value = "[${view::class.qualifiedName.toString()}:onCheckedChanged]\n" + + "isChecked: ${toggleIconView.isChecked}\n" + + "tooltipText: ${toggleIconView.tooltipText}\n" + + "contentDescription: ${toggleIconView.contentDescription}" Log.d("TOGGLEICONVIEW_SAMPLE", value) } } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 35213ec..7642ba5 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -1,5 +1,6 @@ - + - +