ViewTreeObserver viewTreeObserver = view.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); viewWidth = view.getWidth(); viewHeight = view.getHeight(); } }); }
ViewTreeObserver viewTreeObserver = closeButton.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { closeButton.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { closeButton.getViewTreeObserver().removeOnGlobalLayoutListener(this); } // not sure the above is equivalent, but that's beside the point for this example... RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) closeButton.getLayoutParams(); params.setMargins(0, 0, 16, -closeButton.getHeight() / 2); // (int left, int top, int right, int bottom) closeButton.setLayoutParams(params); } }); }
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeGlobalOnLayoutListener(this); viewWidth = mediaGallery.getWidth(); viewHeight = mediaGallery.getHeight(); } }); }
public void enable() { if (Build.VERSION.SDK_INT >= 19) { decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); } }
public KeyboardUtil(Activity act, View contentView) { this.decorView = act.getWindow().getDecorView(); this.contentView = contentView; //only required on newer android versions. it was working on API level 19 if (Build.VERSION.SDK_INT >= 19) { decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); } }
@Override public void onResume() { super.onResume(); final ViewTreeObserver vto = filterContainer.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { height = filterContainer.getHeight(); filterContainer.setVisibility(View.GONE); filterContainer.getLayoutParams().height = 0; filterContainer.requestLayout(); ViewTreeObserver obs = filterContainer.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } }); }
/** * Add an OnGlobalLayoutListener for the view. * <p>This is just a convenience method for using {@code ViewTreeObserver.OnGlobalLayoutListener()}. * This also handles removing listener when onGlobalLayout is called.</p> * * @param view The target view to add global layout listener. * @param runnable Runnable to be executed after the view is laid out. */ public static void addOnGlobalLayoutListener(final View view, final Runnable runnable) { ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { view.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); } runnable.run(); } }); }
public static NavigationBarChangeListener with(View rootView, int orientation) { NavigationBarChangeListener changeListener = new NavigationBarChangeListener(rootView, orientation); rootView.getViewTreeObserver().addOnGlobalLayoutListener(changeListener); return changeListener; }
TextView textView = (TextView)findViewById(R.id.my_textview); ViewTreeObserver observer = textView.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //in here, place the code that requires you to know the dimensions. //this will be called as the layout is finished, prior to displaying. } }
@Override public void onViewCreated(final View inflate) { mLlControlHeight.setClipChildren(false); if (inflate != null) { inflate.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { isLayoutObtain = true; onLayoutObtain(); } }); } }
@Override protected void initView() { tvProgress = (TextView) findViewById(R.id.tv_progress); tvProgress.getViewTreeObserver().addOnGlobalLayoutListener(this); }
@Override public void initToolBar() { mCommonToolbar.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { mCommonToolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { mCommonToolbar.getViewTreeObserver().removeGlobalOnLayoutListener(this); } hideToolBarIfVisible(); } }); showDialog(); }
public void onCreate() { activity.getWindow().setBackgroundDrawable(new ColorDrawable(0x4c000000)); sivViewer = new ScaledImageView(activity); sivViewer.setScaleType(ScaleType.MATRIX); activity.setContentView(sivViewer); if (pic != null) { sivViewer.getViewTreeObserver().addOnGlobalLayoutListener(this); } }
public void onCreate() { activity.getWindow().setBackgroundDrawable(new ColorDrawable(0x4c000000)); sivViewer = new ScaledImageView(activity); sivViewer.setScaleType(ScaleType.MATRIX); activity.setContentView(sivViewer); if (pic != null) { sivViewer.getViewTreeObserver().addOnGlobalLayoutListener(this); } }
public PhotoViewAttacher(ImageView imageView) { mImageView = new WeakReference<ImageView>(imageView); mViewTreeObserver = imageView.getViewTreeObserver(); mViewTreeObserver.addOnGlobalLayoutListener(this); // Make sure we using MATRIX Scale Type setImageViewScaleTypeMatrix(imageView); if (!imageView.isInEditMode()) { setZoomable(true); } }
@Override protected void onCoverAttachedToWindow() { super.onCoverAttachedToWindow(); getGroupValue().registerOnGroupValueUpdateListener(mOnGroupValueUpdateListener); getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mWidth = getView().getWidth(); mHeight = getView().getHeight(); getView().getViewTreeObserver().removeOnGlobalLayoutListener(this); } }); }
private void wrapTabAndBadgeInSameContainer(final BottomBarTab tab) { ViewGroup tabContainer = (ViewGroup) tab.getParent(); tabContainer.removeView(tab); final BadgeContainer badgeContainer = new BadgeContainer(getContext()); badgeContainer.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); badgeContainer.addView(tab); badgeContainer.addView(this); tabContainer.addView(badgeContainer, tab.getIndexInTabContainer()); badgeContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { badgeContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this); adjustPositionAndSize(tab); } }); }
/** * This function should be called in onPrepareOptionsMenu(Menu) of Activity that use * Toolbar as ActionBar, or after inflating menu. */ public void onPrepareMenu(){ if(mGroupChanged || mMenuDataChanged){ mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener); Menu menu = mToolbar.getMenu(); for(int i = 0, count = menu.size(); i < count; i++){ MenuItem item = menu.getItem(i); item.setVisible(item.getGroupId() == mCurrentGroup || item.getGroupId() == 0); } mMenuDataChanged = false; } }
public void show() { final boolean wasShowing = isShowing(); computeContentWidth(); setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); super.show(); if (wasShowing) { // Skip setting up the layout/dismiss listener below. If we were previously // showing it will still stick around. return; } // Make sure we hide if our anchor goes away. // TODO: This might be appropriate to push all the way down to PopupWindow, // but it may have other side effects to investigate first. (Text editing handles, etc.) final ViewTreeObserver vto = getViewTreeObserver(); if (vto != null) vto.addOnGlobalLayoutListener(layoutListener); } }
@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); getViewTreeObserver().addOnGlobalLayoutListener(this); mPreferenceObservable.subscribe(getContext(), (key, contextChanged) -> mVibrationEnabled = Preferences.navigationVibrationEnabled(getContext()), R.string.pref_navigation_vibrate); }