Skip to content

Instantly share code, notes, and snippets.

@louisbl
Forked from orip/ViewGroupUtils.java
Created February 26, 2014 11:23
Show Gist options
  • Save louisbl/9227833 to your computer and use it in GitHub Desktop.
Save louisbl/9227833 to your computer and use it in GitHub Desktop.
package com.onavo.android.common.ui;
import android.view.View;
import android.view.ViewGroup;
import java.util.LinkedList;
import java.util.List;
/**
* Based on http://stackoverflow.com/a/8831593/37020 by by Shlomi Schwartz
* License: MIT
*/
public class ViewGroupUtils {
public static List<View> getViewsByTag(View root, String tag) {
List<View> result = new LinkedList<View>();
if (root instanceof ViewGroup) {
final int childCount = ((ViewGroup) root).getChildCount();
for (int i = 0; i < childCount; i++) {
result.addAll(getViewsByTag(((ViewGroup) root).getChildAt(i), tag));
}
}
final Object rootTag = root.getTag();
// handle null tags, code from Guava's Objects.equal
if (tag == rootTag || (tag != null && tag.equals(rootTag))) {
result.add(root);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment