Skip to content

Instantly share code, notes, and snippets.

@Deliganli
Last active December 6, 2015 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deliganli/40f6cd88391f9d681b14 to your computer and use it in GitHub Desktop.
Save Deliganli/40f6cd88391f9d681b14 to your computer and use it in GitHub Desktop.
/**
* Measures given view and runs the action provided after that
* @param view to be measured
* @param action to be performed
*/
public static void measure(View view, ViewMeasurer action) {
ViewTreeObserver vto = view.getViewTreeObserver();
if (vto.isAlive()) {
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
view.getViewTreeObserver().removeOnPreDrawListener(this);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
if (action == null) return false;
action.onViewMeasured(width, height);
return true;
}
});
}
}
public interface ViewMeasurer {
void onViewMeasured(int width, int height);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment