Skip to content

Instantly share code, notes, and snippets.

@emmaguy
Created February 17, 2020 12:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmaguy/74212914eddcfdff47c46e5c277657ee to your computer and use it in GitHub Desktop.
Save emmaguy/74212914eddcfdff47c46e5c277657ee to your computer and use it in GitHub Desktop.
Look at all Android layout files and collate what value is set for 'android:textColor' and count the usages of each
val textColorsUsed = mutableMapOf<String, Int>()
File("../").walkTopDown().forEach { file ->
if (file.isFile && file.extension == "xml" && file.path.contains("res/layout")) {
val builder: DocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val xmlInput = InputSource(StringReader(file.readText()))
val doc: Document = builder.parse(xmlInput)
val androidViews = XPathFactory.newInstance()
.newXPath()
.evaluate("//*", doc, XPathConstants.NODESET) as NodeList
androidViews.forEach {
it.attributes.getNamedItem("android:textColor")?.let { node ->
textColorsUsed[node.nodeValue] = textColorsUsed.defaultOrIncrement(node.nodeValue)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment