It depends on when you call view.getWidth (). The result will always be 0 if the view is not displayed [onCreate, onResume].
Try calling view.getWidth in
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus);
Hope this helps.
source share