Recursively sets selected flag for all views contained in viewGroup. - Android User Interface

Android examples for User Interface:ViewGroup

Description

Recursively sets selected flag for all views contained in viewGroup.

Demo Code


//package com.java2s;
import android.view.View;
import android.view.ViewGroup;

public class Main {
    /**/*from w ww.ja  va 2  s  . c  o  m*/
     * Recursively sets selected flag for all views contained in viewGroup.
     * Very useful for something like settings screens, when you need to select/unselect all controls
     * inside a layout via Switch, RadioButton or CheckBox
     *
     * @param viewGroup
     *          which children will be selected/unselected
     * @param selected
     *          true or false to set
     */
    public static void setSelectedForAllChildrenRecursively(
            final ViewGroup viewGroup, final boolean selected) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            final View child = viewGroup.getChildAt(i);

            if (child == null)
                continue;

            child.setSelected(selected);

            if (child instanceof ViewGroup) {
                setSelectedForAllChildrenRecursively((ViewGroup) child,
                        selected);
            }
        }
    }
}

Related Tutorials