Get the width of the parent layout
<pre><code>Android Studio 2.0 beta 6
</code></pre>

<p>I am trying to use ViewPropertyAnimator to move a <code>ImageView (ivSettings)</code> inside a toolbar so that it is 20dp from the right and 20dp from the top, from is current location. And move the <code>ImageView (ivSearch)</code> 20dp from the left and top.</p>

<p>The imageViews are contained in a <code>Toolbar</code>.</p>

<p>This is the initial state and I want to move the icons into the upper corners inside the toolbar.</p>

<p><a href="https://i.stack.imgur.com/H01bh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H01bh.png" alt="enter image description here"></a></p>

<p>The code I am using is this to get the width and then subtract a value to get the ivSettings to be 20dp from the right.</p>

<pre><code>final DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final float widthPx = displayMetrics.widthPixels;

ivSearch.animate()
    .setInterpolator(new AccelerateInterpolator())
    .x(20)
    .y(20)
    .setDuration(250)
    .start();

ivSettings.animate()
    .setInterpolator(new AccelerateInterpolator())
    .x(widthPx - 160)
    .y(20)
    .setDuration(250)
    .start();
</code></pre>

<p>However, having tried this on different screen size I can't get the exact width calculation. Is there a better way of doing this?</p>

<p>Many thanks for any suggestions</p>