Example usage for android.view ViewGroup getChildAt

List of usage examples for android.view ViewGroup getChildAt

Introduction

In this page you can find the example usage for android.view ViewGroup getChildAt.

Prototype

public View getChildAt(int index) 

Source Link

Document

Returns the view at the specified position in the group.

Usage

From source file:Main.java

public static ArrayList<View> getViewsByTag(ViewGroup root, String tag) {
    ArrayList<View> views = new ArrayList<View>();
    final int childCount = root.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = root.getChildAt(i);
        if (child instanceof ViewGroup) {
            views.addAll(getViewsByTag((ViewGroup) child, tag));
        }/* ww  w.ja  va 2  s. co  m*/

        final Object tagObj = child.getTag();
        if (tagObj != null && tagObj.equals(tag)) {
            views.add(child);
        }

    }
    return views;
}

From source file:Main.java

/**
 * Disable all the childs of the selected root view
 *
 * @param rootView View to iterate in order to disable all its childs
 * @param alpha    Alpha to set to disabled elements
 *///from  w  w w.ja v  a2  s .com
public static void disableView(ViewGroup rootView, float alpha) {
    int count = rootView.getChildCount();
    View v;
    //Go over the child list of the view and disable all
    for (int i = 0; i < count; i++) {
        v = rootView.getChildAt(i);
        if (v != null) {
            if (v instanceof ViewGroup)
                disableView((ViewGroup) v, alpha);
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO)
                v.setAlpha(alpha);
            v.setEnabled(false);
        }
    }
}

From source file:Main.java

private static View getFirstChildByClassName(ViewGroup parent, String name) {
    View retView = null;//from   ww  w  . j  a  v  a  2  s . c  o  m
    int childCount = parent.getChildCount();

    for (int childIndex = 0; childIndex < childCount; childIndex++) {
        View child = parent.getChildAt(childIndex);

        if (child.getClass().getName().equals(name)) {
            return child;
        }

        if (child instanceof ViewGroup) {
            View v = getFirstChildByClassName((ViewGroup) child, name);

            if (v != null) {
                return v;
            }
        }
    }

    return retView;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static void unbindRecursively(View view) {
    if (view == null)
        return;// w w w . ja  v a2s. co m

    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
        setBackgroundDrawable(view, null);
    }

    if (view instanceof Button) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        setBackgroundDrawable(view, null);
    }

    if (view instanceof ImageView) {
        unbindImageView((ImageView) view);
        setBackgroundDrawable(view, null);
    }

    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            unbindRecursively(viewGroup.getChildAt(i));
        }
        if (viewGroup instanceof AdapterView) {
            ((AdapterView) viewGroup).setAdapter(null);
        } else {
            viewGroup.removeAllViews();
        }
    }
    view.destroyDrawingCache();
    view = null;
}

From source file:Main.java

private static <T extends View> T getFirstChildByInstance(ViewGroup parent, Class<T> instance) {
    View retView = null;/*from   ww w .ja  va 2s.com*/
    int childCount = parent.getChildCount();

    for (int childIndex = 0; childIndex < childCount; childIndex++) {
        View child = parent.getChildAt(childIndex);

        if (instance.isAssignableFrom(child.getClass())) {
            return instance.cast(child);
        }

        if (child instanceof ViewGroup) {
            View v = getFirstChildByInstance((ViewGroup) child, instance);

            if (v != null) {
                return instance.cast(v);
            }
        }
    }

    return instance.cast(retView);
}

From source file:Main.java

/**
 * Recursive crawls the view hierarchy of `viewGroup` in order to find a clickable child and click it.
 *///from  ww w .  ja v  a  2s . c  o m
private static boolean recursiveClickFirstChildView(final @NonNull ViewGroup viewGroup) {
    try {
        boolean continueRecursing = true;

        for (int idx = 0; idx < viewGroup.getChildCount() && continueRecursing; idx++) {
            final View child = viewGroup.getChildAt(idx);
            if (child.hasOnClickListeners()) {
                child.performClick();
                return false;
            } else {
                continueRecursing = recursiveClickFirstChildView((ViewGroup) child);
            }
        }
    } catch (ClassCastException | NullPointerException ignored) {
    }

    return true;
}

From source file:Main.java

private static void cleanViewGroup(View view) {
    if (view != null) {
        if (view instanceof ViewGroup)
            try {
                ViewGroup viewGroup = (ViewGroup) view;
                int childCount = viewGroup.getChildCount();
                for (int index = 0; index < childCount; index++) {
                    View child = viewGroup.getChildAt(index);
                    cleanViewGroup(child);
                }//from w w w .j  a v  a  2  s . com
            } catch (Exception e) {
            }

        cleanView(view);
    }
}

From source file:Main.java

public static void unBindDrawables(View view) {
    if (view != null) {
        try {// w w w .j av  a 2  s . com
            Drawable drawable = view.getBackground();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
                ViewGroup viewGroup = (ViewGroup) view;
                int viewGroupChildCount = viewGroup.getChildCount();
                for (int j = 0; j < viewGroupChildCount; j++) {
                    unBindDrawables(viewGroup.getChildAt(j));
                }
                viewGroup.removeAllViews();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

From source file:Main.java

/**
 * Remove an onclick listener//w w w . j  a v a2 s .c  om
 * @param view the root view of the layout
 */
public static void unBindListener(View view) {
    if (view != null) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                if (view.hasOnClickListeners()) {
                    view.setOnClickListener(null);
                }
            } else {
                view.setOnClickListener(null);
            }

            if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
                ViewGroup viewGroup = (ViewGroup) view;
                int viewGroupChildCount = viewGroup.getChildCount();
                for (int i = 0; i < viewGroupChildCount; i++) {
                    unBindListener(viewGroup.getChildAt(i));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.marc.marclibs.utils.StatusBarCompat.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static View compat(Activity activity, int statusColor) {
    int color = ContextCompat.getColor(activity, R.color.primary_light);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (statusColor != INVALID_VAL) {
            color = statusColor;/*from  w  w w . j a v a  2s  . c  o m*/
        }
        activity.getWindow().setStatusBarColor(color);
        return null;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
        if (statusColor != INVALID_VAL) {
            color = statusColor;
        }
        View statusBarView = contentView.getChildAt(0);
        if (statusBarView != null && statusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) {
            statusBarView.setBackgroundColor(color);
            return statusBarView;
        }
        statusBarView = new View(activity);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                getStatusBarHeight(activity));
        statusBarView.setBackgroundColor(color);
        contentView.addView(statusBarView, lp);
        return statusBarView;
    }
    return null;

}