Skip to content

Commit

Permalink
Add ability to set contentDescription (Closes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgurg committed Aug 6, 2022
1 parent 44190ea commit c8edc3d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` <br /> `isChecked()` | Returns the checked state of the icon | `Boolean` |
| `.isChecked = Boolean` <br /> `setChecked(isChecked: Boolean)` | Sets the checked state of the icon | - |
| `.checkedContentDescription` <br /> `getCheckedContentDescription()` | Returns the checked content description of the icon | `String?` |
| `.checkedContentDescription = String?` <br /> `setCheckedContentDescription(value: String?)` | Sets the checked content description of the icon | - |
| `.uncheckedContentDescription` <br /> `getUncheckedContentDescription()` | Returns the unchecked content description of the icon | `String?` |
| `.uncheckedContentDescription = String?` <br /> `setUncheckedContentDescription(value: String?)` | Sets the unchecked content description of the icon | - |

### Events

Expand Down
33 changes: 33 additions & 0 deletions lib/src/main/java/og/android/lib/toggleiconview/ToggleIconView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,14 +62,30 @@ 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)

try {
// 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()
Expand All @@ -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) {
Expand All @@ -93,6 +125,7 @@ abstract class ToggleIconView @JvmOverloads constructor(
return
}

setContentDescriptionByCheckState(isChecked)
handleCheckState(isChecked)
invokeOnCheckedChangeListener(isChecked)
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
<resources>
<declare-styleable name="ToggleIconView">
<attr name="checked" format="boolean" />
<attr name="checkedContentDescription" format="string" />
<attr name="uncheckedContentDescription" format="string" />
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
11 changes: 9 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -48,7 +49,10 @@
style="@style/DemoSecondaryTitle"
android:text="Sharp" />

<og.android.lib.toggleiconview.sharp.PlayPause style="@style/DemoIcon" />
<og.android.lib.toggleiconview.sharp.PlayPause
style="@style/DemoIcon"
app:checkedContentDescription="Playing..."
app:uncheckedContentDescription="Paused" />
</LinearLayout>

<LinearLayout
Expand All @@ -62,7 +66,10 @@
style="@style/DemoSecondaryTitle"
android:text="Rounded" />

<og.android.lib.toggleiconview.rounded.PlayPause style="@style/DemoIcon" />
<og.android.lib.toggleiconview.rounded.PlayPause
style="@style/DemoIcon"
app:checkedContentDescription="Playing..."
app:uncheckedContentDescription="Paused" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Expand Down

0 comments on commit c8edc3d

Please sign in to comment.