How to get the width of the container in which my view is added in Android

I have an ImageView object inside a LinearLayout. I set the animation in ImageView, which moves the image from point x to y. Now the starting point should be the right corner of the parent container. My question is how to get the size of the container in which the view is present. I do not see the view.getParent () method getWidth () as a hierarchy.

+8
android
source share
3 answers

Perhaps you do not need it, because you can use% to animate like this:

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="150" /> 

+4
source share

Try the following:

 View parent = (View)view.getParent(); int width = parent.getWidth(); 
+25
source share

A call after the view added in the parent view, otherwise you would be releasing java.lang.NullPointerException: trying to call the virtual method 'int android.view.View.getHeight ()'

  int height = ((View) view.getParent()).getHeight(); int width = ((View) view.getParent()).getWidth(); int mHeight = ((View) view.getParent()).getMeasuredHeight(); int mWidth = ((View) view.getParent()).getMeasuredWidth(); 
+1
source share

All Articles