Why Does Calling Getwidth() on a View in Onresume() Return 0

Why does calling getWidth() on a View in onResume() return 0?

A view still hasn't been drawn when onResume() is called, so its width and height are 0. You can "catch" when its size changes using OnGlobalLayoutListener():

yourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {

// Removing layout listener to avoid multiple calls
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
yourView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
else {
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}

populateData();
}
});

For additional info take a look at Android get width returns 0.

If I call getMeasuredWidth() or getWidth() for layout in onResume they return 0

You cannot use the width/height/getMeasuredWidth/getMeasuredHeight on a View before the system renders it (typically from onCreate/onResume).

Simple solution for this is to post a Runnable to the layout. The runnable will be executed after the View has been laid out.

BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout);
BoxesLayout.post(new Runnable() {
@Override
public void run() {
int w = BoxesLayout.getMeasuredWidth();
int h = BoxesLayout.getMeasuredHeight();

...
}
});

getHeight returns 0 for all Android UI objects

In short, the views are not built yet in onCreate(), onStart(), or onResume(). Since they technically don't exist (as far as the ViewGroup is concerned), their dimensions are 0.

In long, you can go here for a better explanation on how to handle it.

How to retrieve the dimensions of a view?

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

You are calling getWidth() too early. The UI has not been sized and laid out on the screen yet.

I doubt you want to be doing what you are doing, anyway -- widgets being animated do not change their clickable areas, and so the button will still respond to clicks in the original orientation regardless of how it has rotated.

That being said, you can use a dimension resource to define the button size, then reference that dimension resource from your layout file and your source code, to avoid this problem.

ImageView.getWidth() returns 0

Where you calling getWidth() and getHeight() on ImageView? If you calling from onCreate() in activity, it won't work. You need to wait for activity window to attached and then call getWidth() and getHeight() on ImageView. You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.

call on onWindowFocusChanged like this way

public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
ImageView img = (ImageView) findViewById(R.id.img);
Log.d(TAG, "width : " + img.getWidth());
}

}

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

You need to use the onSizechanged() method.

This is called during layout when the size of this view has changed

getHeight and getWidth for Custom View returning 0

I'm not sure in your case, but I've used this some time ago. Simply use a ViewObserver. In my case I've used something like this:

final ViewTreeObserver treeObs = dataView.getViewTreeObserver();

treeObs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// This will be called when the layout is finished, just before displaying
...
}
}


Related Topics



Leave a reply



Submit