find a view associated with a popup window - Android User Interface

Android examples for User Interface:PopupWindow

Description

find a view associated with a popup window

Demo Code


import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.Window.Callback;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import com.androidApp.Utility.Constants.Classes;
import com.androidApp.Utility.Constants.Fields;

public class Main{
    /**/*from  w w w .  j  av  a  2  s  .  com*/
     * find a view associated with a popup window
     * @param activity
     * @param popupWindow
     * @return
     */
    public static View findViewForPopup(Activity activity,
            PopupWindow popupWindow) {
        try {
            Class popupViewContainerClass = Class
                    .forName(Constants.Classes.POPUP_VIEW_CONTAINER);
            View[] views = ViewExtractor.getWindowDecorViews();
            if (views != null) {
                int numDecorViews = views.length;

                // iterate through the set of decor windows.  The dialog may already have been presented.
                for (int iView = 0; iView < numDecorViews; iView++) {
                    View v = views[iView];
                    if (DialogUtils.isDialogOrPopup(activity, v)) {
                        if (v.getClass() == popupViewContainerClass) {
                            PopupWindow candPopupWindow = (PopupWindow) ReflectionUtils
                                    .getFieldValue(v,
                                            popupViewContainerClass,
                                            Constants.Classes.THIS);
                            if (candPopupWindow == popupWindow) {
                                return v;
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    /** 
     * is this view in the same context as the activity, but has a different window? Then it is contained in a popup window
     * and may or may not be a dialog.
     * @param a the activity
     * @param v a PhoneWindow$DecorView or PopupContainerView or something like that
     * @return true, or maybe false.  probably true, though
     */
    public static boolean isDialogOrPopup(Activity a, View v) {
        if (v != null) {
            Context viewContext = v.getContext();
            // dialogs use a context theme wrapper, not a context, so we have to extract he context from the theme wrapper's
            // base context. Dialogs can also have nested Context Theme wrappers, so we have to loop to get the "true"
            // base context.
            while (viewContext instanceof ContextThemeWrapper) {
                ContextThemeWrapper ctw = (ContextThemeWrapper) viewContext;
                viewContext = ctw.getBaseContext();
            }
            Context activityContext = a;
            Context activityBaseContext = a.getBaseContext();
            return (activityContext.equals(viewContext) || activityBaseContext
                    .equals(viewContext))
                    && (v != a.getWindow().getDecorView());
        } else {
            return false;
        }
    }
}

Related Tutorials