Skip to content

Commit

Permalink
Add ability to set tooltipText (Closes #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgurg committed Aug 6, 2022
1 parent c8edc3d commit 31c9ab4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ anything you can with `AppCompatImageView`.
| `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` |
| `app:checkedTooltipText` | Sets the initial checked tooltip text of the icon | `String` | `null` |
| `app:uncheckedTooltipText` | Sets the initial unchecked tooltip text of the icon | `String` | `null` |

### Methods

Expand All @@ -76,6 +78,10 @@ anything you can with `AppCompatImageView`.
| `.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 | - |
| `.checkedTooltipText` <br /> `getCheckedTooltipText()` | Returns the checked tooltip text of the icon | `String?` |
| `.checkedTooltipText = String?` <br /> `setCheckedTooltipText(value: String?)` | Sets the checked tooltip text of the icon | - |
| `.uncheckedTooltipText` <br /> `getUncheckedTooltipText()` | Returns the unchecked tooltip text of the icon | `String?` |
| `.uncheckedTooltipText = String?` <br /> `setUncheckedTooltipText(value: String?)` | Sets the unchecked tooltip text 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 @@ -17,6 +17,9 @@ abstract class ToggleIconView @JvmOverloads constructor(
private var mCheckedContentDescription: String? = null
private var mUncheckedContentDescription: String? = null

private var mCheckedTooltipText: String? = null
private var mUncheckedTooltipText: String? = null

private var mIsChecked: Boolean = false

private var mOnCheckedChangeListener: ((view: ToggleIconView, isChecked: Boolean) -> Unit)? = null
Expand Down Expand Up @@ -70,6 +73,14 @@ abstract class ToggleIconView @JvmOverloads constructor(
}
}

private fun setTooltipTextByCheckState(isChecked: Boolean) {
tooltipText = if (isChecked) {
mCheckedTooltipText
} else {
mUncheckedTooltipText
}
}

private fun handleAttributes(attrs: AttributeSet? = null, defStyleAttr: Int = 0) {
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.ToggleIconView, defStyleAttr, 0)

Expand All @@ -85,7 +96,16 @@ abstract class ToggleIconView @JvmOverloads constructor(
val uncheckedContentDescription = typedArray.getString(R.styleable.ToggleIconView_uncheckedContentDescription)
mUncheckedContentDescription = uncheckedContentDescription

// app:checkedTooltipText
val checkedTooltipText = typedArray.getString(R.styleable.ToggleIconView_checkedTooltipText)
mCheckedTooltipText = checkedTooltipText

// app:uncheckedTooltipText
val uncheckedTooltipText = typedArray.getString(R.styleable.ToggleIconView_uncheckedTooltipText)
mUncheckedTooltipText = uncheckedTooltipText

setContentDescriptionByCheckState(checked)
setTooltipTextByCheckState(checked)
handleCheckState(checked)
} finally {
typedArray.recycle()
Expand Down Expand Up @@ -116,6 +136,18 @@ abstract class ToggleIconView @JvmOverloads constructor(
mUncheckedContentDescription = value
}

var checkedTooltipText: String?
get() = mCheckedTooltipText
set(value) {
mCheckedTooltipText = value
}

var uncheckedTooltipText: String?
get() = mUncheckedTooltipText
set(value) {
mUncheckedTooltipText = value
}

var isChecked: Boolean
get() = mIsChecked
set(isChecked) {
Expand All @@ -126,6 +158,7 @@ abstract class ToggleIconView @JvmOverloads constructor(
}

setContentDescriptionByCheckState(isChecked)
setTooltipTextByCheckState(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 @@ -4,5 +4,7 @@
<attr name="checked" format="boolean" />
<attr name="checkedContentDescription" format="string" />
<attr name="uncheckedContentDescription" format="string" />
<attr name="checkedTooltipText" format="string" />
<attr name="uncheckedTooltipText" format="string" />
</declare-styleable>
</resources>
8 changes: 6 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
<og.android.lib.toggleiconview.sharp.PlayPause
style="@style/DemoIcon"
app:checkedContentDescription="Playing..."
app:uncheckedContentDescription="Paused" />
app:checkedTooltipText="Playing..."
app:uncheckedContentDescription="Paused"
app:uncheckedTooltipText="Paused" />
</LinearLayout>

<LinearLayout
Expand All @@ -69,7 +71,9 @@
<og.android.lib.toggleiconview.rounded.PlayPause
style="@style/DemoIcon"
app:checkedContentDescription="Playing..."
app:uncheckedContentDescription="Paused" />
app:checkedTooltipText="Playing..."
app:uncheckedContentDescription="Paused"
app:uncheckedTooltipText="Paused" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Expand Down

0 comments on commit 31c9ab4

Please sign in to comment.