title: Custom view's onMeasure: how to get width based on height
body: <p><em>The previous version of my question was too wordy. People couldn't understand it, so the following is a complete rewrite. See the <a href="https://stackoverflow.com/posts/42390378/edit">edit history</a> if you are interested in the old version.</em></p>
  <p>A <code>RelativeLayout</code> parent sends <a href="https://developer.android.com/reference/android/view/View.MeasureSpec.html" rel="noreferrer"><code>MeasureSpec</code>s</a> to its child view's <code>onMeasure</code> method in order to see how big the child would like to be. This occurs in several passes.</p>
  <h1>My custom view</h1>
  <p>I have a <a href="https://github.com/suragch/mongol-library/blob/master/mongol-library/src/main/java/net/studymongolian/mongollibrary/MongolTextView.java" rel="noreferrer">custom view</a>. As the view's content increases, the view's height increases. When the view reaches the maximum height that the parent will allow, the view's width increases for any additional content (as long as <code>wrap_content</code> was selected for the width). Thus, <strong>the width of the custom view is directly dependant on what parent says the maximum hight must be.</strong></p>
  <p><a href="https://developer.android.com/reference/android/view/View.MeasureSpec.html" rel="noreferrer"><img src="https://i.stack.imgur.com/EYzpL.jpg" alt="enter image description here" /></a></p>
  <h1>An (inharmonious) parent child conversation</h1>
  <p><strong><code>onMeasure</code> pass 1</strong></p>
  <p>The <code>RelativeLayout</code> parent tells my view, &quot;You can be any width <a href="https://developer.android.com/reference/android/view/View.MeasureSpec.html#AT_MOST" rel="noreferrer">up to</a> <code>900</code> and any height <a href="https://developer.android.com/reference/android/view/View.MeasureSpec.html#AT_MOST" rel="noreferrer">up to</a> <code>600</code>. What do you say?&quot;</p>
  <p>My view says, &quot;Well, at that height, I can fit everything with a width of <code>100</code>. So I'll take a width of <code>100</code> and a height of <code>600</code>.&quot;</p>
  <p><strong><code>onMeasure</code> pass 2</strong></p>
  <p>The <code>RelativeLayout</code> parent tells my view, &quot;You told me last time that you wanted a width of <code>100</code>, so let's set that as an <a href="https://developer.android.com/reference/android/view/View.MeasureSpec.html#EXACTLY" rel="noreferrer">exact</a> width. Now, based on that width, what kind of height would you like? Anything <a href="https://developer.android.com/reference/android/view/View.MeasureSpec.html#AT_MOST" rel="noreferrer">up to</a> <code>500</code> is OK.&quot;</p>
  <p>&quot;Hey!&quot; my view replies. &quot;If you're only giving me a maximum hight of <code>500</code>, then <code>100</code> is too narrow. I need a width of <code>200</code> for that height. But fine, have it your way. I won't break the rules <sub>(yet...)</sub>. I'll take a width of <code>100</code> and a height of <code>500</code>.&quot;</p>
  <p><strong>Final result</strong></p>
  <p>The <code>RelativeLayout</code> parent assigns the view a final size of <code>100</code> for the width and <code>500</code> for the height. This is of course too narrow for the view and part of the content gets clipped.</p>
  <p>&quot;Sigh,&quot; thinks my view. &quot;Why won't my parent let me be wider? There is plenty of room. Maybe someone on Stack Overflow can give me some advice.&quot;</p>