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
Android Listview with Header and Footer Buttons
Update SQL Database with Contentvalues and the Update-Method
What Is More Efficient? Static, Data Passing, Shared Preferences, Database...
Create a Custom View by Inflating a Layout
How to Use Firebase Functions to Send Fcm to User
What's "Requestcode" Used for on Pendingintent
How to Solve Render Problem Path.Op() Not Supported
How to Make a Dialog Slide from Bottom to Middle of Screen in Android
How to Avoid Scientific Notation in Double
Android Studio Two Flavors with Different Manifest Files
How to Embed a Youtube Clip in a Webview on Android
CSS Media Queries on Nexus 7, Display Resolution Not Working in Code
Trying to Fix Networkonmainthreadexception But Gives Toast Error
Android Rxjava 2 Junit Test - Getmainlooper in Android.Os.Looper Not Mocked Runtimeexception