Your OnGlobalLayoutListener
is being called every time the screen is laid out, which includes each time the Chronometer
changes (since it changes width, possibly height, etc., and everything invalidates).
Use removeOnGlobalLayoutListener();
in your onGlobalLayout()
method:
ViewTreeObserver observer = timeView.getViewTreeObserver(); // Make this final
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
@SuppressWarnings("deprecation")
public void onGlobalLayout() {
// Your code ...
if (android.os.Build.VERSION.SDK_INT < 16) // Use old, deprecated method
timeView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
else // Use new method
timeView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
Note: You will have to use removeGlobalOnLayoutListener()
instead, for APIs < 16. (The method name was changed in API level 16. They perform in an identical manner.)