get First View By Tag - Android User Interface

Android examples for User Interface:View Tag

Description

get First View By Tag

Demo Code


//package com.java2s;

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

public class Main {
    public static View getFirstViewByTag(View root, Object tag) {
        View result = null;//from ww w  .  ja v a2s  .  c  o m

        if (root != null) {
            result = root.findViewWithTag(tag);
            if (result == null && root instanceof ViewGroup) {
                ViewGroup groupView = (ViewGroup) root;
                final int childCount = groupView.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    result = getFirstViewByTag(groupView.getChildAt(i), tag);
                    if (result != null) {
                        break;
                    }

                }

            }
        }
        return result;

    }
}

Related Tutorials