Get View index in a ViewGroup - Android User Interface

Android examples for User Interface:ViewGroup

Description

Get View index in a ViewGroup

Demo Code

/*//from ww  w. j  a v  a  2  s  .  c  o  m
 * Copyright (C) 2014 Vlad Mihalachi
 *
 * This file is part of Turbo Editor.
 *
 * Turbo Editor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Turbo Editor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import android.support.annotation.NonNull;

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

public class Main {
    public static int indexOf(@NonNull ViewGroup container,
            @NonNull View view) {
        int length = container.getChildCount();
        for (int i = 0; i < length; i++) {
            View child = container.getChildAt(i);
            assert child != null;

            if (child.equals(view)) {
                return i;
            }
        }
        return -1;
    }
}

Related Tutorials