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

Call after view added in a parent view, otherwise you would this exeption java.lang.NullPointerException: Attempt to invoke 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();

Try this:

View parent = (View)view.getParent();
int width = parent.getWidth();

Perhaps you don't need to because you can use % for animation 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" />

Tags:

Android

View