get Views By Tag Prefix - Android User Interface

Android examples for User Interface:View Tag

Description

get Views By Tag Prefix

Demo Code


//package com.java2s;

import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<View> getViewsByTagPrefix(ViewGroup root,
            String tagPrefix) {//from   www  .j  av  a 2  s  . co  m
        List<View> views = new ArrayList<>();
        final int childCount = root.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = root.getChildAt(i);
            if (child instanceof ViewGroup) {
                views.addAll(getViewsByTagPrefix((ViewGroup) child,
                        tagPrefix));
            }

            final Object tagObj = child.getTag();
            if (tagObj != null && tagObj.toString().startsWith(tagPrefix)) {
                views.add(child);
            }
        }

        return views;
    }
}

Related Tutorials