Example usage for android.view ViewGroup getChildCount

List of usage examples for android.view ViewGroup getChildCount

Introduction

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

Prototype

public int getChildCount() 

Source Link

Document

Returns the number of children 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));
        }//from  w  w w. j  a  va  2s  . com

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

    }
    return views;
}

From source file:Main.java

/**
 * Remove an onclick listener/*from  w  ww  .  ja v a 2 s  .c o m*/
 * @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.mobicage.rogerthat.util.TextUtils.java

public static void overrideFonts(final Context context, final View v) {
    try {//from  www. j  av  a 2  s  .  co m
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof EditText) {
            ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/lato_light.ttf"));
            ((EditText) v).setTextColor(ContextCompat.getColor(context, R.color.mc_words_color));
        } else if (v instanceof Button) {
            ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/lato_bold.ttf"));
            ((Button) v).setTextColor(ContextCompat.getColor(context, android.R.color.white));
        } else if (v instanceof TextView) {
            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/lato_light.ttf"));
            ((TextView) v).setTextColor(ContextCompat.getColor(context, R.color.mc_words_color));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Recursive crawls the view hierarchy of `viewGroup` in order to find a clickable child and click it.
 *//*from w w w .  j av a  2 s  .  c  om*/
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

public static void recycleViewGroup(ViewGroup layout) {
    if (layout == null)
        return;//w  ww. j a va  2s  .co m
    synchronized (layout) {
        for (int i = 0; i < layout.getChildCount(); i++) {
            View subView = layout.getChildAt(i);
            if (subView instanceof ViewGroup) {
                recycleViewGroup((ViewGroup) subView);
            } else {
                if (subView instanceof ImageView) {
                    recycleImageView((ImageView) subView);
                }
            }
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static void unbindRecursively(View view) {
    if (view == null)
        return;/*from  w  w w . j  a  v  a 2 s  .  com*/

    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

/**
 * Sets a determined font on a text view element
 *
 * @param context Context in which the TextView can be found
 * @param font    Font to be set in the text view see available fonts as static attributes of this class
 * @param style   {@see Typeface}/*from  ww  w .j a va 2  s.  c om*/
 * @param group   Root layout in which TextView and Buttons will be searched to apply the font
 */
public static void setTypeface(Context context, String font, int style, ViewGroup group) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    int count = group.getChildCount();
    View v;
    for (int i = 0; i < count; i++) {
        v = group.getChildAt(i);
        if (v instanceof TextView)
            ((TextView) v).setTypeface(tf, style);
        else if (v instanceof ViewGroup)
            setTypeface(context, font, style, (ViewGroup) v);
    }
}

From source file:Main.java

/**
 * Sets a determined font on a text view element
 *
 * @param context Context in which the TextView can be found
 * @param font    Font to be set in the text view see available fonts as static attributes of this class
 * @param group   Root layout in which TextView and Buttons will be searched to apply the font
 *///w w  w  .  java2 s . com
public static void setTypeface(Context context, String font, ViewGroup group) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    int count = group.getChildCount();
    View v;
    for (int i = 0; i < count; i++) {
        v = group.getChildAt(i);
        if (v instanceof TextView)
            ((TextView) v).setTypeface(tf);
        else if (v instanceof ViewGroup)
            setTypeface(context, font, (ViewGroup) v);
    }
}

From source file:com.dm.material.dashboard.candybar.helpers.ViewHelper.java

public static void changeSearchViewTextColor(@Nullable View view, int text, int hint) {
    if (view != null) {
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(text);
            ((TextView) view).setHintTextColor(hint);
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                changeSearchViewTextColor(viewGroup.getChildAt(i), text, hint);
            }/*from  w  w  w  .j  a v  a  2  s  . c o  m*/
        }
    }
}

From source file:Main.java

public static void traverseAndRecolor(View root, int color, boolean withStates,
        boolean setClickableItemBackgrounds) {
    Context context = root.getContext();

    if (setClickableItemBackgrounds && root.isClickable()) {
        StateListDrawable selectableItemBackground = new StateListDrawable();
        selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                new ColorDrawable((color & 0xffffff) | 0x33000000));
        selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                new ColorDrawable((color & 0xffffff) | 0x44000000));
        selectableItemBackground.addState(new int[] {}, null);
        root.setBackground(selectableItemBackground);
    }//w ww  .ja v a 2s.c o  m

    if (root instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) root;
        for (int i = 0; i < parent.getChildCount(); i++) {
            traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
        }

    } else if (root instanceof ImageView) {
        ImageView imageView = (ImageView) root;
        Drawable sourceDrawable = imageView.getDrawable();
        if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
            imageView.setImageDrawable(
                    makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
        } else {
            imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

    } else if (root instanceof TextView) {
        TextView textView = (TextView) root;
        if (withStates) {
            int sourceColor = textView.getCurrentTextColor();
            ColorStateList colorStateList = new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed },
                            new int[] { android.R.attr.state_focused }, new int[] {} },
                    new int[] { sourceColor, sourceColor, color });
            textView.setTextColor(colorStateList);
        } else {
            textView.setTextColor(color);
        }

    } else if (root instanceof AnalogClock) {
        AnalogClock analogClock = (AnalogClock) root;
        try {
            Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
            hourHandField.setAccessible(true);
            Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
            minuteHandField.setAccessible(true);
            Field dialField = AnalogClock.class.getDeclaredField("mDial");
            dialField.setAccessible(true);
            BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
            if (hourHand != null) {
                Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                d.setCallback(analogClock);
                hourHandField.set(analogClock, d);
            }
            BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
            if (minuteHand != null) {
                Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                d.setCallback(analogClock);
                minuteHandField.set(analogClock, d);
            }
            BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
            if (dial != null) {
                Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                d.setCallback(analogClock);
                dialField.set(analogClock, d);
            }
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        } catch (ClassCastException ignored) {
        } // TODO: catch all exceptions?
    }
}