find View By Id recursively - Android User Interface

Android examples for User Interface:View Find

Description

find View By Id recursively

Demo Code


//package com.java2s;

import android.view.View;
import android.view.ViewGroup;

public class Main {

    public static View findViewById(Object view, int id) {
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            View child = group.findViewById(id);
            if (child == null) {
                for (int i = 0; i < group.getChildCount(); i++) {
                    view = group.getChildAt(i);
                    if (view instanceof ViewGroup) {
                        if (findViewById((ViewGroup) view, id) == null) {
                            return null;
                        }/*  ww w  .j  a v a2 s . c o m*/
                    }
                }
            }
            return child;
        } else if (view instanceof View) {
            if (((View) view).getId() == id) {
                return (View) view;
            }
        } else {
            return null;
        }
        return null;
    }
}

Related Tutorials