Skip to content

Instantly share code, notes, and snippets.

@kobeumut
Created November 22, 2020 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobeumut/3ec4bda5dc3118de30b707da71b3c0de to your computer and use it in GitHub Desktop.
Save kobeumut/3ec4bda5dc3118de30b707da71b3c0de to your computer and use it in GitHub Desktop.
Find View with tag recursively in Kotlin
fun ViewGroup.findViewWithTagRecursively(tag: Any): List<View>? {
val allViews: MutableList<View> = ArrayList<View>()
val childCount: Int = this.childCount
for (i in 0 until childCount) {
val childView: View = this.getChildAt(i)
if (childView is ViewGroup) {
childView.findViewWithTagRecursively(tag)?.let { allViews.addAll(it) }
} else {
val tagView: Any? = childView.tag
if (tagView != null && tagView == tag) allViews.add(childView)
}
}
return allViews
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment