Skip to content

Instantly share code, notes, and snippets.

@sunwicked
Created December 24, 2013 10:06
Show Gist options
  • Select an option

  • Save sunwicked/8111175 to your computer and use it in GitHub Desktop.

Select an option

Save sunwicked/8111175 to your computer and use it in GitHub Desktop.
Get dimensions of a view after measurement of view is completed , solves getting view's dimensions as 0 in onCreate()
//root of layout
final View view = findViewById(R.id.root);
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver vto = view.getViewTreeObserver();
width = profile_Lin.getWidth();
height = profile_Lin.getHeight();
profile_Lin.setBackgroundDrawable(createSelector(getResources().getColor(
R.color.profile_new)));
vto.removeGlobalOnLayoutListener(this);
}
});
@sunwicked
Copy link
Author

Better approach would be to get the size in onWindowFocusChanged()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment