Java tutorial
//package com.java2s; //License from project: Open Source License import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.View; import android.view.ViewGroup; public class Main { @Nullable public static <T> T firstChildOfType(@NonNull View root, @NonNull Class<T> type) { if (type.isInstance(root)) { return type.cast(root); } if (root instanceof ViewGroup) { final ViewGroup rootGroup = (ViewGroup) root; for (int i = 0; i < rootGroup.getChildCount(); i++) { final View child = rootGroup.getChildAt(i); final T childResult = firstChildOfType(child, type); if (childResult != null) { return childResult; } } } return null; } }