Android examples for User Interface:View Parent
Dumps the hierarchy of a view
import java.lang.reflect.Field; import java.util.HashMap; import android.view.View; import android.view.ViewGroup; public class Main { private static HashMap<Integer, String> mIdValues; /**/*from www. j a va2 s. co m*/ * Dumps the hierarchy of a view * * @param R * The ID reference of your project, must be R.id.class * @param depth * The initial tab depth (usually 0) * @param parent * The root view * @return A line separated, tabbed structure of the view's hierarchy */ public static String dump(Class R, int depth, View parent) { if (mIdValues == null) { getIdValues(R); } String retStr = ""; retStr += "\n" + depth + " " + parent.toString(); if (mIdValues.get(parent.getId()) != null) { retStr += " id: R.id." + mIdValues.get(parent.getId()); } if (parent instanceof ViewGroup) { int childCount = ((ViewGroup) parent).getChildCount(); for (int index = 0; index < childCount; index++) { retStr += dump(R, depth + 1, ((ViewGroup) parent).getChildAt(index)); } } return retStr; } private static void getIdValues(Class R) { mIdValues = new HashMap<Integer, String>(); for (Field f : R.getFields()) { try { mIdValues.put((Integer) R.getField(f.getName()).get(Integer.class), f.getName()); } catch (Exception e) { e.printStackTrace(); } } } }