Example usage for android.view ViewGroup getViewTreeObserver

List of usage examples for android.view ViewGroup getViewTreeObserver

Introduction

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

Prototype

public ViewTreeObserver getViewTreeObserver() 

Source Link

Document

Returns the ViewTreeObserver for this view's hierarchy.

Usage

From source file:Main.java

@SuppressLint("NewApi")
public static void setViewDimens(Context context, final ViewGroup parent, final View view, final int width,
        final int height) {
    parent.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @SuppressWarnings("deprecation")
        @Override/*  ww  w .  j  a  v a2  s .  c  om*/
        public void onGlobalLayout() {
            LayoutParams params = view.getLayoutParams();
            if (params == null) {
                params = new LayoutParams(width, height);
                view.setLayoutParams(params);
            } else {
                params.width = width;
                params.height = height;
            }

            // remove OnGlobalLayoutListener, check Android version
            // too
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                parent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            else
                parent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });
}

From source file:com.givewaygames.transition.TransitionManager.java

private static void sceneChangeRunTransition(final ViewGroup sceneRoot, final Transition transition) {
    if (transition != null) {
        final ViewTreeObserver observer = sceneRoot.getViewTreeObserver();
        final ViewTreeObserver.OnPreDrawListener listener = new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                sceneRoot.getViewTreeObserver().removeOnPreDrawListener(this);
                sPendingTransitions.remove(sceneRoot);
                // Add to running list, handle end to remove it
                final ArrayMap<ViewGroup, ArrayList<Transition>> runningTransitions = getRunningTransitions();
                ArrayList<Transition> currentTransitions = runningTransitions.get(sceneRoot);
                ArrayList<Transition> previousRunningTransitions = null;
                if (currentTransitions == null) {
                    currentTransitions = new ArrayList<Transition>();
                    runningTransitions.put(sceneRoot, currentTransitions);
                } else if (currentTransitions.size() > 0) {
                    previousRunningTransitions = new ArrayList<Transition>(currentTransitions);
                }/*from   ww  w .  j a v a 2s  .c o m*/
                currentTransitions.add(transition);
                transition.addListener(new Transition.TransitionListenerAdapter() {
                    @Override
                    public void onTransitionEnd(Transition transition) {
                        ArrayList<Transition> currentTransitions = runningTransitions.get(sceneRoot);
                        currentTransitions.remove(transition);
                    }
                });
                transition.captureValues(sceneRoot, false);
                if (previousRunningTransitions != null) {
                    for (Transition runningTransition : previousRunningTransitions) {
                        runningTransition.resume();
                    }
                }
                transition.playTransition(sceneRoot);

                return true;
            }
        };
        observer.addOnPreDrawListener(listener);
    }
}

From source file:android.support.transition.TransitionManager.java

private static void sceneChangeRunTransition(final ViewGroup sceneRoot, final Transition transition) {
    if (transition != null && sceneRoot != null) {
        MultiListener listener = new MultiListener(transition, sceneRoot);
        sceneRoot.addOnAttachStateChangeListener(listener);
        sceneRoot.getViewTreeObserver().addOnPreDrawListener(listener);
    }//from  www.  ja  v  a 2s  .  c o m
}

From source file:android.support.transition.TransitionManagerPort.java

private static void sceneChangeRunTransition(final ViewGroup sceneRoot, final TransitionPort transition) {
    if (transition != null && sceneRoot != null) {
        MultiListener listener = new MultiListener(transition, sceneRoot);
        sceneRoot.addOnAttachStateChangeListener(listener);
        sceneRoot.getViewTreeObserver().addOnPreDrawListener(listener);
    }// w w w .  j ava2 s . co m
}

From source file:com.transitionseverywhere.TransitionManager.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void sceneChangeRunTransition(final ViewGroup sceneRoot, final Transition transition) {
    if (transition != null && sceneRoot != null && isTransitionsAllowed()) {
        ViewGroupOverlayUtils.initializeOverlay(sceneRoot);
        MultiListener listener = new MultiListener(transition, sceneRoot);
        sceneRoot.addOnAttachStateChangeListener(listener);
        sceneRoot.getViewTreeObserver().addOnPreDrawListener(listener);
    } else {/*from   w w w . j a v  a2s .c o  m*/
        sPendingTransitions.remove(sceneRoot);
    }
}

From source file:org.onebusaway.android.ui.TripPlanActivity.java

@Override
protected void onResume() {
    super.onResume();

    Bundle bundle = mBuilder.getBundle();
    boolean newItineraries = false;

    // see if there is data from intent
    Intent intent = getIntent();/*www .  jav a 2s .  c  om*/

    if (intent != null && intent.getExtras() != null) {

        OTPConstants.Source source = (OTPConstants.Source) intent
                .getSerializableExtra(OTPConstants.INTENT_SOURCE);
        if (source != null) {

            // Copy planning params - necessary if this intent came from a notification.
            if (source == OTPConstants.Source.NOTIFICATION) {
                new TripRequestBuilder(intent.getExtras()).copyIntoBundle(bundle);
            }

            ArrayList<Itinerary> itineraries = (ArrayList<Itinerary>) intent.getExtras()
                    .getSerializable(OTPConstants.ITINERARIES);

            if (itineraries != null) {
                bundle.putSerializable(OTPConstants.ITINERARIES, itineraries);
                newItineraries = true;
            }

            if (intent.getIntExtra(PLAN_ERROR_CODE, -1) != -1) {
                bundle.putSerializable(SHOW_ERROR_DIALOG, true);
                bundle.putInt(PLAN_ERROR_CODE, intent.getIntExtra(PLAN_ERROR_CODE, 0));
                bundle.putString(PLAN_ERROR_URL, intent.getStringExtra(PLAN_ERROR_URL));
            }

            setIntent(null);
            bundle.putBoolean(REQUEST_LOADING, false);
            mRequestLoading = false;

        }
    }

    if (mRequestLoading || bundle.getBoolean(REQUEST_LOADING)) {
        showProgressDialog();
    }

    // Check which fragment to create
    boolean haveTripPlan = bundle.getSerializable(OTPConstants.ITINERARIES) != null;

    TripPlanFragment fragment = (TripPlanFragment) getSupportFragmentManager()
            .findFragmentById(R.id.trip_plan_fragment_container);
    if (fragment == null) {
        fragment = new TripPlanFragment();
        fragment.setArguments(bundle);
        fragment.setListener(this);
        getSupportFragmentManager().beginTransaction().add(R.id.trip_plan_fragment_container, fragment)
                .commit();
    }

    mPanel = (SlidingUpPanelLayout) findViewById(R.id.trip_plan_sliding_layout);

    mPanel.setEnabled(haveTripPlan);

    if (haveTripPlan) {
        initResultsFragment();
        if (bundle.getBoolean(PANEL_STATE_EXPANDED) || newItineraries) {
            mPanel.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
        }
    }

    // show error dialog if necessary
    if (bundle.getBoolean(SHOW_ERROR_DIALOG)) {
        int planErrorCode = intent.getIntExtra(PLAN_ERROR_CODE, 0);
        String planErrorUrl = intent.getStringExtra(PLAN_ERROR_URL);
        showFeedbackDialog(planErrorCode, planErrorUrl);
    }

    if (fragment != null && intent != null) {
        fragment.setPlanErrorUrl(intent.getStringExtra(PLAN_ERROR_URL));
        fragment.setPlanRequestUrl(intent.getStringExtra(PLAN_REQUEST_URL));
    }

    // Set the height of the panel after drawing occurs.
    final ViewGroup layout = (ViewGroup) findViewById(R.id.trip_plan_fragment_container);
    ViewTreeObserver vto = layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            int viewHeight = mPanel.getHeight();
            int height = layout.getMeasuredHeight();
            mPanel.setPanelHeight(viewHeight - height);
        }
    });
}

From source file:com.amazon.android.uamp.ui.PlaybackOverlayFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    if (rootView == null) {
        Log.e(TAG, "could not find root view for this fragment");
        return null;
    }/* www  .j a  v  a 2  s  .c  o  m*/
    final ViewGroup layout = (ViewGroup) rootView.findViewById(R.id.details_fragment_root);
    if (layout != null) {
        ViewTreeObserver viewTreeObserver = layout.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                setActionsInitialStates();
                // Continue getting called until we are able to initialize the control views
                // as we have to watch the details fragment this may take a few updates.
                if (areControlViewsInitialized()) {
                    layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
    }

    return rootView;
}

From source file:net.tjado.passwdsafe.PasswdSafeOpenFileFragment.java

public void resetOverflowButton(final Activity activity) {
    final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
    final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
    final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override/* ww  w.ja v a  2  s. c  o  m*/
        public void onGlobalLayout() {
            final ArrayList<View> outViews = new ArrayList<View>();
            decorView.findViewsWithText(outViews, overflowDescription,
                    View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
            if (outViews.isEmpty()) {
                return;
            }
            AppCompatImageView overflow = (AppCompatImageView) outViews.get(0);

            overflow.setImageDrawable(itsOriginalDrawable);
            removeOnGlobalLayoutListener(decorView, this);
        }
    });
}

From source file:net.tjado.passwdsafe.PasswdSafeOpenFileFragment.java

public void setOverflowButton(final Activity activity) {
    final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
    final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
    final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override/*  ww  w.ja v a2 s.c  om*/
        public void onGlobalLayout() {
            final ArrayList<View> outViews = new ArrayList<View>();
            decorView.findViewsWithText(outViews, overflowDescription,
                    View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
            if (outViews.isEmpty()) {
                return;
            }
            AppCompatImageView overflow = (AppCompatImageView) outViews.get(0);

            itsOriginalDrawable = overflow.getDrawable();

            overflow.setImageDrawable(PasswdSafeUtil.scaleImage(
                    activity.getResources().getDrawable(R.drawable.icon_yubico), 0.09f, getResources()));
            overflow.setColorFilter(activity.getResources().getColor(R.color.menu_icon_color));
            removeOnGlobalLayoutListener(decorView, this);
        }
    });
}

From source file:com.android.incallui.CallCardFragment.java

@Override
public void onResume() {
    super.onResume();
    // If the previous launch animation is still running, cancel it so that we don't get
    // stuck in an intermediate animation state.
    if (mAnimatorSet != null && mAnimatorSet.isRunning()) {
        mAnimatorSet.cancel();/*from   w w  w.  j a va 2  s . c o m*/
    }

    mIsLandscape = getResources().getBoolean(R.bool.is_layout_landscape);
    mHasLargePhoto = getResources().getBoolean(R.bool.has_large_photo);

    final ViewGroup parent = ((ViewGroup) mPrimaryCallCardContainer.getParent());
    final ViewTreeObserver observer = parent.getViewTreeObserver();
    parent.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ViewTreeObserver viewTreeObserver = observer;
            if (!viewTreeObserver.isAlive()) {
                viewTreeObserver = parent.getViewTreeObserver();
            }
            viewTreeObserver.removeOnGlobalLayoutListener(this);
            mFloatingActionButtonController.setScreenWidth(parent.getWidth());
            updateFabPosition();
        }
    });

    updateColors();
}