Java Code Examples for android.view.View.getViewTreeObserver()

The following are Jave code examples for showing how to use getViewTreeObserver() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: ListItemView   File: ViewUtils.java   Source Code and License Vote up 6 votes
/**
 * Executes the given {@link Runnable} when the view is laid out
 **/
public static void onLaidOut(final View view, final Runnable runnable) {
    if (isLaidOut(view)) {
        runnable.run();
        return;
    }

    final ViewTreeObserver observer = view.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final ViewTreeObserver trueObserver;

            if (observer.isAlive()) {
                trueObserver = observer;
            } else {
                trueObserver = view.getViewTreeObserver();
            }

            removeGlobalLayoutObserver(trueObserver, this);
            runnable.run();
        }
    });
}
 
Example 2
Project: MusicX-music-player   File: ATEUtil.java   Source Code and License Vote up 6 votes
public static void setOverflowButtonColor(@NonNull Activity activity,
                                          @Nullable Toolbar toolbar,
                                          final @ColorInt int color) {
    if (toolbar != null && toolbar.getTag() != null && ATE.IGNORE_TAG.equals(toolbar.getTag()))
        return; // ignore tag was set, don't update the overflow
    final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
    final View target = toolbar != null ? toolbar :
            (ViewGroup) activity.getWindow().getDecorView();
    final ViewTreeObserver viewTreeObserver = target.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final ArrayList<View> outViews = new ArrayList<>();
            target.findViewsWithText(outViews, overflowDescription,
                    View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
            if (outViews.isEmpty()) return;
            final AppCompatImageView overflow = (AppCompatImageView) outViews.get(0);
            overflow.setImageDrawable(TintHelper.createTintedDrawable(overflow.getDrawable(), color));
            removeOnGlobalLayoutListener(target, this);
        }
    });
}
 
Example 3
Project: OpenLive-Android   File: BaseActivity.java   Source Code and License Vote up 6 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final View layout = findViewById(Window.ID_ANDROID_CONTENT);
    ViewTreeObserver vto = layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            initUIandEvent();
        }
    });
}
 
Example 4
Project: Phial   File: ShareView.java   Source Code and License Vote up 6 votes
private void animateViewAfterMeasured(final View view) {
    final ViewTreeObserver vto = view.getViewTreeObserver();
    if (!vto.isAlive()) {
        sharePickerView.setVisibility(GONE);
        return;
    }

    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (view.getHeight() > 0 || view.getWidth() > 0) {
                if (vto.isAlive()) {
                    vto.removeGlobalOnLayoutListener(this);
                    animateAppear();
                }
            }
        }
    });
}
 
Example 5
Project: MyFlightbookAndroid   File: ActFlightMap.java   Source Code and License Vote up 6 votes
public void onMapReady(GoogleMap googleMap) {
    if (m_gMap == null) {
        m_gMap = googleMap;

        GoogleMap map = getMap();
        if (map == null) {
            MFBUtil.Alert(this, getString(R.string.txtError), getString(R.string.errNoGoogleMaps));
            finish();
            return;
        }

        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        UiSettings settings = map.getUiSettings();
        settings.setCompassEnabled(false);
        settings.setRotateGesturesEnabled(false);
        settings.setScrollGesturesEnabled(true);
        settings.setZoomControlsEnabled(false);
        settings.setZoomGesturesEnabled(true);

        View mapView = getFragmentManager().findFragmentById(R.id.mfbMap).getView();
        if (mapView != null && mapView.getViewTreeObserver() != null && mapView.getViewTreeObserver().isAlive()) {
            mapView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }

        map.setOnMarkerClickListener(this);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            map.setMyLocationEnabled(true);
        }
        map.setOnMapLongClickListener(this);

        updateMapElements();
    }
}
 
Example 6
Project: Ghost-Android   File: KeyboardUtils.java   Source Code and License Vote up 6 votes
public static void addKeyboardVisibilityChangedListener(
        @NonNull OnKeyboardVisibilityChangedListener listener, @NonNull Activity activity) {
    final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content))
            .getChildAt(0);
    ViewTreeObserver viewTreeObserver = activityRootView.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        private final int ESTIMATED_KEYBOARD_SIZE_DP = 100;
        private final Rect r = new Rect();

        @Override
        public void onGlobalLayout() {
            float estimatedKeyboardHeight = DeviceUtils.dpToPx(ESTIMATED_KEYBOARD_SIZE_DP);
            activityRootView.getWindowVisibleDisplayFrame(r);
            int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
            boolean isVisible = (heightDiff >= estimatedKeyboardHeight);
            listener.onVisibilityChanged(isVisible);
        }
    });
}
 
Example 7
Project: GitHub   File: ScrollUtils.java   Source Code and License Vote up 5 votes
/**
 * Add an OnGlobalLayoutListener for the view.
 * This is just a convenience method for using {@code ViewTreeObserver.OnGlobalLayoutListener()}.
 * This also handles removing listener when onGlobalLayout is called.
 *
 * @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() {
        @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();
        }
    });
}
 
Example 8
Project: MyFire   File: DisplayUtil.java   Source Code and License Vote up 5 votes
/**
 * 直接获取控件的宽、高
 * @param view
 * @return int[]
 */
public static int[] getWidgetWH(final View view){
    ViewTreeObserver vto2 = view.getViewTreeObserver();
    vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
        	view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });
	return new int[]{view.getWidth(),view.getHeight()};
}
 
Example 9
Project: MyFire   File: DisplayUtil.java   Source Code and License Vote up 5 votes
/**
 * 直接获取控件的宽、高
 * @param view
 * @return int[]
 */
public static int getViewHeight(final View view){
	ViewTreeObserver vto2 = view.getViewTreeObserver();
	vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
		@Override
		public void onGlobalLayout() {
			view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
		}
	});
	return view.getHeight();
}
 
Example 10
Project: MyFire   File: DisplayUtil.java   Source Code and License Vote up 5 votes
/**
 * 直接获取控件的宽、高
 * @param view
 * @return int[]
 */
public static int getViewWidth(final View view){
	ViewTreeObserver vto2 = view.getViewTreeObserver();
	vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
		@Override
		public void onGlobalLayout() {
			view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
		}
	});
	return view.getWidth();
}
 
Example 11
Project: airgram   File: ActionBarPopupWindow.java   Source Code and License Vote up 5 votes
private void registerListener(View anchor) {
    if (mSuperScrollListener != null) {
        ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver() : null;
        if (vto != mViewTreeObserver) {
            if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
                mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
            }
            if ((mViewTreeObserver = vto) != null) {
                vto.addOnScrollChangedListener(mSuperScrollListener);
            }
        }
    }
}
 
Example 12
Project: airgram   File: EmojiView.java   Source Code and License Vote up 5 votes
private void registerListener(View anchor) {
    if (mSuperScrollListener != null) {
        ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver() : null;
        if (vto != mViewTreeObserver) {
            if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
                mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
            }
            if ((mViewTreeObserver = vto) != null) {
                vto.addOnScrollChangedListener(mSuperScrollListener);
            }
        }
    }
}
 
Example 13
Project: boohee_v5.6   File: MenuPopupHelper.java   Source Code and License Vote up 5 votes
public boolean tryShow() {
    boolean addGlobalListener = false;
    this.mPopup = new ListPopupWindow(this.mContext, null, this.mPopupStyleAttr, this.mPopupStyleRes);
    this.mPopup.setOnDismissListener(this);
    this.mPopup.setOnItemClickListener(this);
    this.mPopup.setAdapter(this.mAdapter);
    this.mPopup.setModal(true);
    View anchor = this.mAnchorView;
    if (anchor == null) {
        return false;
    }
    if (this.mTreeObserver == null) {
        addGlobalListener = true;
    }
    this.mTreeObserver = anchor.getViewTreeObserver();
    if (addGlobalListener) {
        this.mTreeObserver.addOnGlobalLayoutListener(this);
    }
    this.mPopup.setAnchorView(anchor);
    this.mPopup.setDropDownGravity(this.mDropDownGravity);
    if (!this.mHasContentWidth) {
        this.mContentWidth = measureContentWidth();
        this.mHasContentWidth = true;
    }
    this.mPopup.setContentWidth(this.mContentWidth);
    this.mPopup.setInputMethodMode(2);
    this.mPopup.show();
    this.mPopup.getListView().setOnKeyListener(this);
    return true;
}
 
Example 14
Project: Sanxing   File: OperateItemActivityBase.java   Source Code and License Vote up 5 votes
protected void animationReveal(Bundle savedInstanceState) {
    cx = getResources().getDisplayMetrics().widthPixels - 120;
    cy = getResources().getDisplayMetrics().heightPixels - 180;
    final View rootLayout = getWindow().getDecorView().getRootView();
    if (savedInstanceState == null) {
        rootLayout.setVisibility(View.INVISIBLE);
        ViewTreeObserver viewTreeObserver = rootLayout.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    float finalRadius = Math.max(rootLayout.getWidth(), rootLayout.getHeight());
                    // create the animator for this view (the start radius is zero)
                    Animator circularReveal = ViewAnimationUtils.createCircularReveal(rootLayout, cx, cy, 0, finalRadius);
                    circularReveal.setDuration(revealTime);

                    // make the view visible and start the animation
                    rootLayout.setVisibility(View.VISIBLE);
                    circularReveal.start();
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                }
            });
        }
    }
}
 
Example 15
Project: MaterialMasterDetail   File: ViewUtils.java   Source Code and License Vote up 5 votes
/**
 * Executes the given {@link java.lang.Runnable} when the view is laid out
 **/
public static void onLaidOut(final View view, final Runnable runnable) {
    if (isLaidOut(view)) {
        runnable.run();
        return;
    }

    final ViewTreeObserver observer = view.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final ViewTreeObserver trueObserver;

            if (observer.isAlive()) {
                trueObserver = observer;
            } else {
                trueObserver = view.getViewTreeObserver();
            }

            if (Build.VERSION.SDK_INT >= 16) {
                trueObserver.removeOnGlobalLayoutListener(this);
            } else {
                //noinspection deprecation
                trueObserver.removeGlobalOnLayoutListener(this);
            }
            runnable.run();
        }
    });
}
 
Example 16
Project: Mire   File: TransitionAnimation.java   Source Code and License Vote up 5 votes
public static MoveData startAnimation(Context context, final View toView, Bundle transitionBundle, Bundle savedInstanceState, final int duration, final TimeInterpolator interpolator) {
    final TransitionData transitionData = new TransitionData(context, transitionBundle);
    if (transitionData.imageFilePath != null) {
        setImageToView(toView, transitionData.imageFilePath);
    }
    final MoveData moveData = new MoveData();
    moveData.toView = toView;
    moveData.duration = duration;
    if (savedInstanceState == null) {

        ViewTreeObserver observer = toView.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                toView.getViewTreeObserver().removeOnPreDrawListener(this);

                int[] screenLocation = new int[2];
                toView.getLocationOnScreen(screenLocation);
                moveData.leftDelta = transitionData.thumbnailLeft - screenLocation[0];
                moveData.topDelta = transitionData.thumbnailTop - screenLocation[1];

                moveData.widthScale = (float) transitionData.thumbnailWidth / toView.getWidth();
                moveData.heightScale = (float) transitionData.thumbnailHeight / toView.getHeight();

                runEnterAnimation(moveData, interpolator);

                return true;
            }
        });
    }
    return moveData;
}
 
Example 17
Project: react-native-webgl-view-shot   File: RNWebGLTextureView.java   Source Code and License Vote up 5 votes
public RNWebGLTextureView(ReadableMap config, View view) {
    super(config, view.getWidth(), view.getHeight());
    boolean continuous = config.hasKey("continuous") && config.getBoolean("continuous");
    yflip = config.hasKey("yflip") && config.getBoolean("yflip");
    this.view = view;
    if (continuous) {
        viewTreeObserver = view.getViewTreeObserver();
        viewTreeObserver.addOnDrawListener(this);
    }
    else {
        viewTreeObserver = null;
    }
    this.runOnGLThread(this);
}
 
Example 18
Project: AndroidGeneralUtils   File: ViewUtil.java   Source Code and License Vote up 5 votes
/**
 * Remove global listener set to view
 *
 * @param v        The view from which the global listener to be removed
 * @param listener The listener to be removed
 */
public static void removeGloabalLayoutListener(View v, ViewTreeObserver.OnGlobalLayoutListener listener) {
    if (v == null || v.getViewTreeObserver() == null) {
        return;
    }
    if (Build.VERSION.SDK_INT > 16) {
        v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
    } else {
        v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
    }
}
 
Example 19
Project: PlusGram   File: ActionBarPopupWindow.java   Source Code and License Vote up 5 votes
private void registerListener(View anchor) {
    if (mSuperScrollListener != null) {
        ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver() : null;
        if (vto != mViewTreeObserver) {
            if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
                mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
            }
            if ((mViewTreeObserver = vto) != null) {
                vto.addOnScrollChangedListener(mSuperScrollListener);
            }
        }
    }
}
 
Example 20
Project: PlusGram   File: EmojiView.java   Source Code and License Vote up 5 votes
private void registerListener(View anchor) {
    if (mSuperScrollListener != null) {
        ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver() : null;
        if (vto != mViewTreeObserver) {
            if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
                mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
            }
            if ((mViewTreeObserver = vto) != null) {
                vto.addOnScrollChangedListener(mSuperScrollListener);
            }
        }
    }
}