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();
...
}
});
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() returns 0 if set by android:layout_width=match_parent
A view's size isn't available until after onMeasure()
, particularly if its set to wrap_content
, or fill_parent
.
You should either access the size in or after onMeasure()
in the View code, or in a a Layout Tree Observer:
LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});
You will also need to add android:id="@+id/mylayout"
to your LinearLayout for the second one to work.
HorizontalScrollView.getMeasuredWidth() returns 0
Ok, I see the problem. You create a HorizontalScrollView, add a child to it, and then immediately try to get its measured width.
You cannot do this. You must add the horizontal scroll view to an existing already-drawn view in your activity first, because otherwise it doesn't have set dimensions yet.
Think about how would it know how many pixels WRAP_CONTENT will set the dimension to before its laid out in your view? If you add it to an existing, already-laid-out view in your activity, then that WRAP_CONTENT will actually get converted to some height.
It looks like you kind-of have a loop - horizontalScroll's dimensions depend on its content (WRAP_CONTENT), yet the content's (LinearLayout's) dimensions depend on the horizontalScroll's dimensions. This does not make sense. Perhaps try MATCH_PARENT for at least the width dimensions of your horizontal scroll view. Then, make sure to not look at dimensions until the view has been drawn.
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());
}
}
Related Topics
How to Do Circular Scrolling on Viewpager
Differencebetween Src and Background of Imageview
Android: Launch Activity from Clickable Text
Default Activity Not Found in Android Studio
Save State of Activity When Orientation Changes Android
Launch Android Application from a Browser Link
Android/Java: Transition/Migration from Proguard to R8
Broadcastreceiver as Inner Class
Immersive Mode Navigation Becomes Sticky After Volume Press or Minimise-Restore
Android Activity Has Leaked Window Com.Android.Internal.Policy.Impl.Phonewindow$Decorview Issue
Do Geofences Remain Active in Android After a Device Reboot
How to Finish an Android Application
Equivalent of Clean & Build in Android Studio
Pick a Number and Name from Contacts List in Android App