Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Gets a list of all views of a given type, rooted at the given parent.
     * <p>
     * This method will recurse down through all {@link ViewGroup} instances looking for
     * {@link View} instances of the supplied class type. Specifically it will use the
     * {@link Class#isAssignableFrom(Class)} method as the test for which views to add to the list,
     * so if you provide {@code View.class} as your type, you will get every view. The parent itself
     * will be included also, should it be of the right type.
     * <p>
     * This call manipulates the ui, and as such should only be called from the application's main
     * thread.
     */
    private static <T extends View> List<T> getAllViews(final Class<T> clazz, final View parent) {
        List<T> results = new ArrayList<T>();
        if (parent.getClass().equals(clazz)) {
            results.add(clazz.cast(parent));
        }
        if (parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) parent;
            for (int i = 0; i < viewGroup.getChildCount(); ++i) {
                results.addAll(getAllViews(clazz, viewGroup.getChildAt(i)));
            }
        }
        return results;
    }
}