Java tutorial
//package com.java2s; import android.view.View; import android.view.ViewGroup; public class Main { private static <T extends View> T getFirstChildByInstance(ViewGroup parent, Class<T> instance) { View retView = null; int childCount = parent.getChildCount(); for (int childIndex = 0; childIndex < childCount; childIndex++) { View child = parent.getChildAt(childIndex); if (instance.isAssignableFrom(child.getClass())) { return instance.cast(child); } if (child instanceof ViewGroup) { View v = getFirstChildByInstance((ViewGroup) child, instance); if (v != null) { return instance.cast(v); } } } return instance.cast(retView); } }