Java Code Examples for android.view.ViewTreeObserver#OnGlobalLayoutListener

The following examples show how to use android.view.ViewTreeObserver#OnGlobalLayoutListener . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BannerBindingWrapper.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public ViewTreeObserver.OnGlobalLayoutListener inflate(
    Map<Action, View.OnClickListener> actionListeners,
    View.OnClickListener dismissOnClickListener) {

  View root = inflater.inflate(R.layout.banner, null);
  bannerRoot = root.findViewById(R.id.banner_root);
  bannerContentRoot = root.findViewById(R.id.banner_content_root);
  bannerBody = root.findViewById(R.id.banner_body);
  bannerImage = root.findViewById(R.id.banner_image);
  bannerTitle = root.findViewById(R.id.banner_title);

  if (message.getMessageType().equals(MessageType.BANNER)) {
    BannerMessage bannerMessage = (BannerMessage) message;
    setMessage(bannerMessage);
    setLayoutConfig(config);
    setSwipeDismissListener(dismissOnClickListener);
    setActionListener(actionListeners.get(bannerMessage.getAction()));
  }
  return null;
}
 
Example 2
Source File: FlowLayoutManager.java    From AndroidLibs with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onAttachedToWindow(final RecyclerView view) {
	super.onAttachedToWindow(view);
	this.recyclerView = view;
	layoutHelper = new LayoutHelper(this, recyclerView);
	cacheHelper = new CacheHelper(flowLayoutOptions.itemsPerLine, layoutHelper.visibleAreaWidth());
	if (layoutHelper.visibleAreaWidth() == 0) {
		if (globalLayoutListener == null) {
			globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
				@Override
				public void onGlobalLayout() {
					view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
					globalLayoutListener = null;
					cacheHelper.contentAreaWidth(layoutHelper.visibleAreaWidth());
				}
			};
		}
		view.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
	}

}
 
Example 3
Source File: ViewHelper.java    From DMusic with Apache License 2.0 5 votes vote down vote up
public static void removeOnGlobalLayoutListener(View view, ViewTreeObserver.OnGlobalLayoutListener victim) {
    if (view == null || victim == null) {
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        view.getViewTreeObserver().removeOnGlobalLayoutListener(victim);
    } else {
        view.getViewTreeObserver().removeGlobalOnLayoutListener(victim);
    }
}
 
Example 4
Source File: ViewTreeObserverCompat.java    From android-signaturepad with Apache License 2.0 5 votes vote down vote up
/**
 * Remove a previously installed global layout callback.
 * @param observer the view observer
 * @param victim the victim
 */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static void removeOnGlobalLayoutListener(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener victim) {
    // Future (API16+)...
    if (Build.VERSION.SDK_INT >= 16) {
        observer.removeOnGlobalLayoutListener(victim);
    }
    // Legacy...
    else {
        observer.removeGlobalOnLayoutListener(victim);
    }
}
 
Example 5
Source File: FirebaseInAppMessagingDisplayTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void streamListener_onNotifiedModalMessage_setsLayoutListener() {
  resumeActivity(activity);

  ViewTreeObserver.OnGlobalLayoutListener mockListener =
      mock(ViewTreeObserver.OnGlobalLayoutListener.class);
  modalBindingWrapper.setLayoutListener(mockListener);

  listener.displayMessage(MODAL_MESSAGE_MODEL, callbacks);

  modalBindingWrapper.getImageView().getViewTreeObserver().dispatchOnGlobalLayout();
  verify(mockListener).onGlobalLayout();
}
 
Example 6
Source File: ImageBindingWrapper.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public ViewTreeObserver.OnGlobalLayoutListener inflate(
    Map<Action, View.OnClickListener> actionListeners,
    View.OnClickListener dismissOnClickListener) {
  View v = inflater.inflate(R.layout.image, null);
  imageRoot = v.findViewById(R.id.image_root);
  imageContentRoot = v.findViewById(R.id.image_content_root);
  imageView = v.findViewById(R.id.image_view);
  collapseButton = v.findViewById(R.id.collapse_button);

  // Setup ImageView.
  imageView.setMaxHeight(config.getMaxImageHeight());
  imageView.setMaxWidth(config.getMaxImageWidth());
  if (message.getMessageType().equals(MessageType.IMAGE_ONLY)) {
    ImageOnlyMessage msg = (ImageOnlyMessage) message;
    imageView.setVisibility(
        (msg.getImageData() == null || TextUtils.isEmpty(msg.getImageData().getImageUrl()))
            ? View.GONE
            : View.VISIBLE);
    imageView.setOnClickListener(actionListeners.get(msg.getAction()));
  }

  // Setup dismiss button.
  imageRoot.setDismissListener(dismissOnClickListener);
  collapseButton.setOnClickListener(dismissOnClickListener);
  return null;
}
 
Example 7
Source File: KeyboardUtil.java    From JKeyboardPanelSwitch with Apache License 2.0 5 votes vote down vote up
/**
 * Recommend invoked by {@link Activity#onCreate(Bundle)}
 * For align the height of the keyboard to {@code target} as much as possible.
 * For save the refresh the keyboard height to shared-preferences.
 *
 * @param activity contain the view
 * @param target   whose height will be align to the keyboard height.
 * @param lis      the listener to listen in: keyboard is showing or not.
 * @see #saveKeyboardHeight(Context, int)
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity,
                                                             IPanelHeightTarget target,
                                                             /* Nullable */
                                                             OnKeyboardShowingListener lis) {
    final ViewGroup contentView = activity.findViewById(android.R.id.content);
    final boolean isFullScreen = ViewUtil.isFullScreen(activity);
    final boolean isTranslucentStatus = ViewUtil.isTranslucentStatus(activity);
    final boolean isFitSystemWindows = ViewUtil.isFitsSystemWindows(activity);

    // get the screen height.
    final Display display = activity.getWindowManager().getDefaultDisplay();
    final int screenHeight;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        final Point screenSize = new Point();
        display.getSize(screenSize);
        screenHeight = screenSize.y;
    } else {
        //noinspection deprecation
        screenHeight = display.getHeight();
    }

    ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new KeyboardStatusListener(
            isFullScreen,
            isTranslucentStatus,
            isFitSystemWindows,
            contentView,
            target,
            lis,
            screenHeight);

    contentView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
    return globalLayoutListener;
}
 
Example 8
Source File: ShimmerFrameLayout.java    From oneHookLibraryAndroid with Apache License 2.0 5 votes vote down vote up
private ViewTreeObserver.OnGlobalLayoutListener getLayoutListener() {
    return new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            boolean animationStarted = mAnimationStarted;
            resetAll();
            if (mAutoStart || animationStarted) {
                startShimmerAnimation();
            }
        }
    };
}
 
Example 9
Source File: CardBindingWrapper.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public ViewTreeObserver.OnGlobalLayoutListener inflate(
    Map<Action, View.OnClickListener> actionListeners,
    View.OnClickListener dismissOnClickListener) {

  View root = inflater.inflate(R.layout.card, null);
  bodyScroll = root.findViewById(R.id.body_scroll);
  primaryButton = root.findViewById(R.id.primary_button);
  secondaryButton = root.findViewById(R.id.secondary_button);
  imageView = root.findViewById(R.id.image_view);
  messageBody = root.findViewById(R.id.message_body);
  messageTitle = root.findViewById(R.id.message_title);
  cardRoot = root.findViewById(R.id.card_root);
  cardContentRoot = root.findViewById(R.id.card_content_root);

  if (message.getMessageType().equals(MessageType.CARD)) {
    cardMessage = (CardMessage) message;
    setMessage(cardMessage);
    setImage(cardMessage);
    setButtons(actionListeners);
    setLayoutConfig(config);
    setDismissListener(dismissOnClickListener);
    setViewBgColorFromHex(cardContentRoot, cardMessage.getBackgroundHexColor());
  }
  return layoutListener;
}
 
Example 10
Source File: ApiCompatibilityUtils.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @see android.view.ViewTreeObserver#removeOnGlobalLayoutListener()
 */
@SuppressWarnings("deprecation")
public static void removeOnGlobalLayoutListener(
        View view, ViewTreeObserver.OnGlobalLayoutListener listener) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
    } else {
        view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
    }
}
 
Example 11
Source File: WorkWorldDetailsAdapter.java    From imsdk-android with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void removeGlobalOnLayoutListener(ViewTreeObserver obs, ViewTreeObserver.OnGlobalLayoutListener listener) {
    if (obs == null)
        return;
    if (Build.VERSION.SDK_INT < 16) {
        obs.removeGlobalOnLayoutListener(listener);
    } else {
        obs.removeOnGlobalLayoutListener(listener);
    }
}
 
Example 12
Source File: Views.java    From droidconat-2016 with Apache License 2.0 5 votes vote down vote up
@TargetApi(JELLY_BEAN)
public static void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener listener) {
    if (App.isCompatible(JELLY_BEAN)) {
        viewTreeObserver.removeOnGlobalLayoutListener(listener);
    } else {
        viewTreeObserver.removeGlobalOnLayoutListener(listener);
    }
}
 
Example 13
Source File: KeyboardUtil.java    From imsdk-android with MIT License 5 votes vote down vote up
/**
 * Recommend invoked by {@link Activity#onCreate(Bundle)}
 * For align the height of the keyboard to {@code target} as much as possible.
 * For save the refresh the keyboard height to shared-preferences.
 *
 * @param activity contain the view
 * @param target   whose height will be align to the keyboard height.
 * @param lis      the listener to listen in: keyboard is showing or not.
 * @see #saveKeyboardHeight(Context, int)
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity,
                                                             IPanelHeightTarget target,
                                                             /* Nullable */
                                                             OnKeyboardShowingListener lis) {
    final ViewGroup contentView = activity.findViewById(android.R.id.content);
    final boolean isFullScreen = ViewUtil.isFullScreen(activity);
    final boolean isTranslucentStatus = ViewUtil.isTranslucentStatus(activity);
    final boolean isFitSystemWindows = ViewUtil.isFitsSystemWindows(activity);

    // get the screen height.
    final Display display = activity.getWindowManager().getDefaultDisplay();
    final int screenHeight;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        final Point screenSize = new Point();
        display.getSize(screenSize);
        screenHeight = screenSize.y;
    } else {
        //noinspection deprecation
        screenHeight = display.getHeight();
    }

    ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new KeyboardStatusListener(
            isFullScreen,
            isTranslucentStatus,
            isFitSystemWindows,
            contentView,
            target,
            lis,
            screenHeight);

    contentView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
    return globalLayoutListener;
}
 
Example 14
Source File: ViewUtils.java    From ListItemView with Apache License 2.0 5 votes vote down vote up
public static void removeGlobalLayoutObserver(final ViewTreeObserver observer,
        ViewTreeObserver.OnGlobalLayoutListener layoutListener) {
    if (Build.VERSION.SDK_INT < 16) {
        observer.removeGlobalOnLayoutListener(layoutListener);
    } else {
        observer.removeOnGlobalLayoutListener(layoutListener);
    }
}
 
Example 15
Source File: Utils.java    From google-io-2014-compat with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static void removeOnGlobalLayoutListenerCompat(View v, ViewTreeObserver.OnGlobalLayoutListener listener) {
    if (hasJellyBean()) {
        v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
    } else {
        v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
    }
}
 
Example 16
Source File: SoftKeyboardDetector.java    From ucar-weex-core with Apache License 2.0 4 votes vote down vote up
public DefaultUnRegister(Activity activity, ViewTreeObserver.OnGlobalLayoutListener listener) {
    this.activityRef = new WeakReference<>(activity);
    this.listenerRef = new WeakReference<>(listener);
}
 
Example 17
Source File: BubbleDialog.java    From HappyBubble with Apache License 2.0 4 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (mBubbleLayout == null)
        {
            mBubbleLayout = new BubbleLayout(getContext());
        }
        if (mAddView != null)
        {
            mBubbleLayout.addView(mAddView);
        }
        setContentView(mBubbleLayout);

        final Window window = getWindow();
        if (window == null) return;
        if (mSoftShowUp)
        {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        }
        window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        onAutoPosition();

        setLook();
//        mBubbleLayout.post(new Runnable()
//        {
//            @Override
//            public void run()
//            {
//                dialogPosition();
//            }
//        });
        mBubbleLayout.measure(0, 0);
        dialogPosition();

        mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
        {
            int lastWidth, lastHeight;
            @Override
            public void onGlobalLayout()
            {
                if (lastWidth == mBubbleLayout.getMeasuredWidth() && lastHeight == mBubbleLayout.getMeasuredHeight()) return;
                dialogPosition();
                lastWidth = mBubbleLayout.getMeasuredWidth();
                lastHeight = mBubbleLayout.getMeasuredHeight();
            }
        };

        mBubbleLayout.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);


        mBubbleLayout.setOnClickEdgeListener(new BubbleLayout.OnClickEdgeListener()
        {
            @Override
            public void edge()
            {
                if (BubbleDialog.this.mCancelable)
                {
                    dismiss();
                }
            }
        });
    }
 
Example 18
Source File: AndroidLauncher.java    From riiablo with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate (Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
  config.useAccelerometer        = false;
  config.useGyroscope            = false;
  config.useCompass              = false;
  config.useRotationVectorSensor = false;
  config.maxSimultaneousSounds   = 16;

  FileHandle home = new FileHandle(getContext().getExternalFilesDir(null));
  final Client client = new Client(home, 360);
  initialize(client, config);

  Cvars.Client.Display.StatusBar.addStateListener(new CvarStateAdapter<Boolean>() {
    @Override
    public void onChanged(Cvar<Boolean> cvar, Boolean from, final Boolean to) {
      getHandler().post(new Runnable() {
        @Override
        public void run() {
          if (to.booleanValue()) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
          } else {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
          }
        }
      });
    }
  });

  final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      Rect visibleDisplayFrame = new Rect();
      getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleDisplayFrame);
      int height = /*content.getRootView().getHeight() -*/ Gdx.graphics.getHeight() - visibleDisplayFrame.height();
      client.updateScreenBounds(0, height, 0, 0); // TODO: other params are unused for now
    }
  };
  final View content = getWindow().getDecorView().findViewById(android.R.id.content);
  content.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
  Gdx.app.addLifecycleListener(new LifecycleListener() {
    @Override
    public void pause() {}

    @Override
    public void resume() {}

    @Override
    public void dispose() {
      content.getViewTreeObserver().removeOnGlobalLayoutListener(globalLayoutListener);
    }
  });
}
 
Example 19
Source File: KeyboardUtil.java    From imsdk-android with MIT License 4 votes vote down vote up
/**
 * @see #attach(Activity, IPanelHeightTarget, OnKeyboardShowingListener)
 */
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity,
                                                             IPanelHeightTarget target) {
    return attach(activity, target, null);
}
 
Example 20
Source File: ViewCanaryInternal.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
void start(ViewCanary viewCanary, ViewCanaryConfig config) {
    Handler handler = ThreadUtil.createIfNotExistHandler(VIEW_CANARY_HANDLER);
    callbacks = new SimpleActivityLifecycleCallbacks() {

        private Map<Activity, ViewTreeObserver.OnGlobalLayoutListener> mOnGlobalLayoutListenerMap = new HashMap<>();
        private Map<Activity, List<List<ViewIdWithSize>>> mRecentLayoutListRecords = new HashMap<>();

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            super.onActivityCreated(activity, savedInstanceState);
            mRecentLayoutListRecords.put(activity, new ArrayList<>());
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            super.onActivityDestroyed(activity);
            mRecentLayoutListRecords.remove(activity);
        }

        @Override
        public void onActivityResumed(Activity activity) {
            super.onActivityResumed(activity);
            ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView();
            Runnable callback = inspectInner(new WeakReference<>(activity), viewCanary, config, mRecentLayoutListRecords);
            ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = () -> {
                handler.removeCallbacks(callback);
                handler.postDelayed(callback, INSPECT_DELAY_TIME_MILLIS);
            };
            mOnGlobalLayoutListenerMap.put(activity, onGlobalLayoutListener);
            parent.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }

        @Override
        public void onActivityPaused(Activity activity) {
            super.onActivityPaused(activity);
            ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = mOnGlobalLayoutListenerMap.remove(activity);
            ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView();
            if (onGlobalLayoutListener != null) {
                parent.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
            }
        }
    };
    GodEye.instance().getApplication().registerActivityLifecycleCallbacks(callbacks);
}