Skip to content

Commit

Permalink
chore(module-test): add custom checkable
Browse files Browse the repository at this point in the history
  • Loading branch information
Irineu333 committed Nov 21, 2023
1 parent 4622402 commit 80a6bc7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/src/main/java/com/neo/test/checkable/CustomCheckable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.neo.test.checkable

import android.content.Context
import android.util.AttributeSet
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.Checkable
import androidx.appcompat.widget.AppCompatTextView

class CustomCheckable(
context: Context,
attrs: AttributeSet? = null,
) : AppCompatTextView(context, attrs), Checkable {

private var checked = false

private val CHECKED_STATE_SET = intArrayOf(
android.R.attr.state_checked
)

init {
isClickable = true
}

override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo) {
super.onInitializeAccessibilityNodeInfo(info)

info.isCheckable = true
info.isChecked = isChecked
}

override fun setChecked(checked: Boolean) {
this.checked = checked

refreshDrawableState()
}

override fun isChecked(): Boolean {
return checked
}

override fun toggle() {
isChecked = !isChecked
}

override fun performClick(): Boolean {
toggle()

return super.performClick()
}

override fun onCreateDrawableState(extraSpace: Int): IntArray? {
val drawableState = super.onCreateDrawableState(extraSpace + 1)

if (isChecked) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET)
}

return drawableState
}
}
26 changes: 26 additions & 0 deletions test/src/main/res/layout/fragment_switches.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@

</androidx.appcompat.widget.LinearLayoutCompat>

<com.google.android.material.divider.MaterialDivider
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginVertical="8dp" />

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="CustomCheckable" />

<com.neo.test.checkable.CustomCheckable
android:text="CustomCheckable"
android:gravity="center_vertical"
android:drawableStart="?android:attr/listChoiceIndicatorMultiple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</androidx.appcompat.widget.LinearLayoutCompat>

</androidx.appcompat.widget.LinearLayoutCompat>

</ScrollView>

0 comments on commit 80a6bc7

Please sign in to comment.