Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
final ArrayList<View> allViews = getAllChildren(findViewById(R.id.mainLayout));
if(!allViews.contains(findViewById(R.id.mainLayout)))allViews.add(findViewById(R.id.mainLayout));
findViewById(R.id.mainLayout).getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Layout has happened here.
ArrayList<View> nl = new ArrayList<>();
ArrayList<String> sl = new ArrayList<>();
for(int j = 0; j < allViews.size(); j++) {
try {
View v = findViewById(allViews.get(j).getId());
if(v == null) continue;
if(nl.contains(v)) continue;
ArrayList<View> allTouchables = v.getTouchables();
for (int i = 0; i < allTouchables.size(); i++) {
View touchable = allTouchables.get(i);
//if(touchable.getX() == 0.0 && touchable.getY() == 0.0) continue;
int loc[] = new int[2];
touchable.getLocationOnScreen(loc);
loc[0] += Math.abs(touchable.getLeft() - touchable.getRight())/2;
loc[1] += Math.abs(touchable.getTop() - touchable.getBottom())/2;
if(!sl.contains("ID:"
+ getResources().getResourceName(touchable.getId())
+ ":IDend" + "Xval:" + loc[0] + ":Xend"
+ "|Yval:" + loc[1] + ":Yend")) {
sl.add("ID:"
+ getResources().getResourceName(touchable.getId())
+ ":IDend" + "Xval:" + loc[0] + ":Xend"
+ "|Yval:" + loc[1] + ":Yend");
}
}
nl.add(v);
} catch (Exception e) {
}
}
for(int i = 0; i < sl.size(); i++){
Log.d("monkeyrunner_locations", "monkeyrunner_locations:"+sl.get(i));
}
// Don't forget to remove your listener when you are done with it.
findViewById(R.id.mainLayout).getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
/* .... */
//TODO This part of the code was found on stackoverflow : https://stackoverflow.com/questions/8395168/android-get-children-inside-a-view
private ArrayList<View> getAllChildren(View v) {
if (!(v instanceof ViewGroup)) {
ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
return viewArrayList;
}
ArrayList<View> result = new ArrayList<View>();
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
viewArrayList.addAll(getAllChildren(child));
result.addAll(viewArrayList);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment