Getheight Returns 0 for All Android UI Objects

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.

getWidth() and getHeight() return 0 value

Use OnLayoutChangeListener:

myView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
myView.removeOnLayoutChangeListener(this);

// get dimensions now
}
});

This is a better approach than setting an arbitrary delay. You will get a callback when the bounds of the View changes due to layout processing. So you will get the proper dimensions inside the onLayoutChange callback. Don't forget the remove the listener after the first callback.

layout getHeight() and getWidth() return 0 when getting image from camera

You should call getWidth() and getHeight() methods after or inside onLayout() event. Here is more detailed description of that process
How Android Draws Views.

Try to call frame.getWidth() in this method, and you'll see that it's result differs from 0.

frame.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top,
int right, int bottom,
int oldLeft, int oldTop,
int oldRight, int oldBottom) {
int w = frame.getWidth();
}
});

RelativeLayout.getWidth returning 0 when layout width is 720

You should get width here when Your UI has been loaded and then only width can be invoked of layout.:

@Override
public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);
//paste your code to get your layout params.
}

getWidth and getHeight sometimes return 0, sometimes not on 4.4.2

This is the solution I came up with. If someone has different or better, please post it.

Since the problem only exists in 4.4.2 (and future, I assume), I used an if and the SDK_INT to do something different for 19 and greater. The issue appears to be that images don't have a height and width until displayed, and in 4.4.2 sometimes for a short time after it is displayed the getWidth() and getHeight() functions will still return 0. However, assuming I am doing no scaling on the image, I can use the getBackground().getIntrinsicHeight() and getBackground().getIntrinsicWidth() and use that instead. To get the screen width and height I used getSize(point) on the Display, and since I'm using the screen size and not the size of the Relative View I need to get the padding of the parent in order to center it properly.

LayoutParams params = (LayoutParams) mMenuButton.getLayoutParams();
if (android.os.Build.VERSION.SDK_INT >= 19) {
RelativeLayout mButtonsLayout = (RelativeLayout) getParent();
WindowManager wm = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
params.topMargin = (size.y - mMenuButton.getBackground().getIntrinsicHeight()) / 2 - mButtonsLayout.getPaddingTop();
params.leftMargin = (size.x - mMenuButton.getBackground().getIntrinsicWidth()) / 2;
}
else {
params.topMargin = (getHeight() - mMenuButton.getHeight()) / 2;
params.leftMargin = (getWidth() - mMenuButton.getWidth()) / 2;
}
mMenuButton.setLayoutParams(params);

There is probably a better solution out there, and I hope someone posts it, but this solution is working for me at the moment.



Related Topics



Leave a reply



Submit