Skip to content

Instantly share code, notes, and snippets.

@fulcrum6378
Created June 8, 2022 22:08
Show Gist options
  • Save fulcrum6378/9217945bd31f8745a138a65f6e6371c6 to your computer and use it in GitHub Desktop.
Save fulcrum6378/9217945bd31f8745a138a65f6e6371c6 to your computer and use it in GitHub Desktop.
ViewGroup analyser for Android in Kotlin
@file:Suppress("MemberVisibilityCanBePrivate", "unused")
package <COM.EXAMPLE.APP>
import android.view.View
import android.view.ViewGroup
import androidx.core.view.children
import com.google.gson.GsonBuilder
class ViewGroupWalk(vg: ViewGroup) {
val data = vg.walk()
private fun View.walk(): ViewInfo = ViewInfo(
javaClass.name, if (this@walk is ViewGroup) children.toList().map { it.walk() } else null)
fun gson(): String = GsonBuilder().setPrettyPrinting().create().toJson(data)
inner class ViewInfo(val name: String, val children: List<ViewInfo>?)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment