Last active
November 28, 2017 12:12
-
-
Save PhB-fr/e43ba4a5dbd1ed7e1038b9a2393eceeb to your computer and use it in GitHub Desktop.
Java / Android | Iterates through a given Layout tree, looking for views matching a certain type (TextView for example).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Iterates through the given Layout, looking for TextView | |
--------------------------------- | |
Author : Philippe Bartolini (PhB-fr @ GitHub) | |
Link : https://gist.github.com/PhB-fr/e43ba4a5dbd1ed7e1038b9a2393eceeb | |
Yes another iterator ;) I think it is very adaptable | |
*/ | |
public void MyIterator(View thisView){ | |
ViewGroup thisViewGroup = null; | |
boolean isTextView = false; | |
int childrenCount = 0; | |
try { // Test by casting if the node is a ViewGroup and if it has children | |
thisViewGroup = (ViewGroup) thisView; | |
childrenCount = thisViewGroup.getChildCount(); | |
} | |
catch (Exception e){ | |
} | |
if(childrenCount == 0){ // Test by casting if this node is a TextView | |
try { | |
isTextView = ((TextView) thisView).getText() != null; // You can adapt it to your own neeeds. | |
} | |
catch (Exception e){ | |
} | |
if(isTextView){ | |
// do something | |
} | |
} | |
else { | |
for(int i = 0; i < childrenCount; i++){ // looping in the tree | |
MyIterator(thisViewGroup.getChildAt(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment