is View Attached To Decoration View - Android User Interface

Android examples for User Interface:View

Description

is View Attached To Decoration View

Demo Code


//package com.java2s;

import android.app.Activity;

import android.view.View;

import android.view.ViewParent;

public class Main {
    public static boolean isViewAttachedToDecorView(View view) {
        if (!(view.getContext() instanceof Activity)) {
            return true;
        }//from   w w w .  j  a  v a2 s .  c om
        View decorView = ((Activity) view.getContext()).getWindow()
                .getDecorView();
        if (view == decorView) {
            return true;
        }
        if (view.getWindowToken() != null
                && view.getWindowToken() != decorView.getWindowToken()) {
            // The view is not in the same window with activity. It's probably in a Dialog.
            return true;
        }
        ViewParent parent = view.getParent();
        while (parent != null) {
            if (parent == decorView) {
                return true;
            }
            parent = parent.getParent();
        }
        return false;
    }
}

Related Tutorials