Android Development
Programming Issues
Java
View Objects
Debugging Methods

View's getWidth() and getHeight() returns 0

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If getWidth() or getHeight() returns 0, the view usually has not been laid out yet. The view exists, but Android has not finished the measure-and-layout pass that determines its final size.

That is why this problem shows up so often in onCreate, right after inflate, or immediately after setContentView. The code is running before the view has real dimensions.

Why Size Is Not Ready Yet

Android view size is determined in stages:

  • inflate or create the view
  • measure it
  • lay it out
  • draw it

getWidth() and getHeight() only return useful values after layout. Before that, zero is normal.

This code is a classic example of calling too early:

java
1@Override
2protected void onCreate(Bundle savedInstanceState) {
3    super.onCreate(savedInstanceState);
4    setContentView(R.layout.activity_main);
5
6    View box = findViewById(R.id.box);
7    System.out.println(box.getWidth());   // often 0
8    System.out.println(box.getHeight());  // often 0
9}

Nothing is wrong with the methods. The timing is wrong.

Use post for a Simple Fix

One easy solution is to schedule work on the view after the current layout work completes:

java
1View box = findViewById(R.id.box);
2
3box.post(() -> {
4    int width = box.getWidth();
5    int height = box.getHeight();
6    System.out.println("Width = " + width + ", Height = " + height);
7});

This is often the cleanest fix when you just need the size once after layout.

Use a Layout Listener When You Need More Control

If you want explicit notification when layout happens, attach a listener:

java
1View box = findViewById(R.id.box);
2
3box.getViewTreeObserver().addOnGlobalLayoutListener(
4    new ViewTreeObserver.OnGlobalLayoutListener() {
5        @Override
6        public void onGlobalLayout() {
7            box.getViewTreeObserver().removeOnGlobalLayoutListener(this);
8
9            int width = box.getWidth();
10            int height = box.getHeight();
11            System.out.println("Width = " + width + ", Height = " + height);
12        }
13    }
14);

This is useful when you need to react exactly when layout completes, or when size changes matter.

Prefer Measured APIs Only When You Understand Them

You may see getMeasuredWidth() and getMeasuredHeight() mentioned as alternatives. They are related, but they are not a universal escape hatch.

Measured size is part of the measure phase, not necessarily the final laid-out size. For many problems, what you actually want is the post-layout width and height, so post or a layout listener remains the safer answer.

Example: Acting on Size After Layout

Suppose you want to size another view relative to the first one:

java
1View source = findViewById(R.id.source);
2View target = findViewById(R.id.target);
3
4source.post(() -> {
5    int width = source.getWidth();
6    ViewGroup.LayoutParams params = target.getLayoutParams();
7    params.width = width / 2;
8    target.setLayoutParams(params);
9});

This works because the source view now has a real width.

Why onWindowFocusChanged Is Not the Best General Fix

Some older advice suggests using onWindowFocusChanged. It does happen after layout in many cases, but it is a broad lifecycle callback and can run multiple times for reasons unrelated to the one view you care about.

For one view's size, post, OnGlobalLayoutListener, or other view-scoped callbacks are usually cleaner.

Common Pitfalls

The biggest pitfall is reading width and height in onCreate and assuming zero means the layout XML is broken. Usually the view just has not been laid out yet.

Another common issue is using getMeasuredWidth() as a magic replacement without understanding that measured size and final layout size are not always the same thing.

People also forget to remove global layout listeners when they only need one callback. That can lead to repeated work.

Finally, if the view is GONE, detached, or constrained to zero size by layout rules, then 0 may be the real final size rather than a timing problem.

Summary

  • 'getWidth() and getHeight() return 0 when called before layout finishes.'
  • 'View.post(...) is a simple way to run code after the view has been laid out.'
  • 'OnGlobalLayoutListener is useful when you need more explicit layout callbacks.'
  • 'getMeasuredWidth() is not a universal replacement for final layout size.'
  • Sometimes zero is correct, so also check visibility and layout constraints.

Course illustration
Course illustration

All Rights Reserved.