title: Android ImageView - Fill width and Resize to Maintain Aspect Ratio
body: <p>I have a row <code>View</code> that I am using in a <code>ListView</code>.</p>
  
  <p>This <code>RowView</code> consists of an <code>ImageView</code> on the left and a <code>TextView</code> on the right in a horizontal <code>LinearLayout</code> where the image is given 40% of the space and the text the remaining 60%.</p>
  
  <p>I want the layout to handle the resizing of the image in the <code>ImageView</code> in the following manner:</p>
  
  <ol>
  <li>Stretch the image horizontally so that it fills up given 40% of the <code>LinearLayout</code></li>
  <li>Resize the <code>ImageView</code> vertically to maintain the original aspect ratio</li>
  </ol>
  
  <p>This is my approach to the layouts:</p>
  
  <pre><code>protected class RowView extends LinearLayout {
  public ImageView iv = null;
  public TextView tv = null;
  
  private LinearLayout.LayoutParams ivlp = null;
  private LinearLayout.LayoutParams tvlp = null;
  
  public RowView(Context ct) {
      super(ct);
  
      setOrientation(LinearLayout.HORIZONTAL);
  
      // Create the views
      iv = new ImageView(ct);
      ivlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.4f);
      iv.setLayoutParams(ivlp);
      iv.setAdjustViewBounds(true);
      iv.setScaleType(ScaleType.FIT_XY);
  
      tv = new TextView(ct);
      tvlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.4f);
      tv.setLayoutParams(tvlp);
      tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
  
      // Add the views
      addView(iv);
      addView(tv);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The result I am getting is something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/PTRkH.png" alt="enter image description here"></p>
  
  <p>From what I understand:</p>
  
  <ul>
  <li>The <code>MATCH_PARENT</code> is correctly forcing the image to horizontally stretch to fill the 40% space</li>
  <li>The <code>ImageView.setAdjustViewBounds(true)</code> is supposed to allow any resizing of the <code>ImageView</code> that is necessary which is what I want. The problem is none of the <code>ImageView.ScaleType</code>'s seem to do what I want</li>
  <li><code>ImageView.ScaleType(ScaleType.FIT_XY)</code> is not what I want because it doesn't maintain the aspect ratio. But all the others that do, don't seem to stretch the height of the <code>ImageView</code> (which is making <code>ImageView.setAdjustViewBounds(true)</code> pretty much useless).</li>
  </ul>
  
  <p>What am I doing wrong? It seems wrong that something this simple could be so difficult?</p>
  
  <p>Or do I actually have to go to the effort of extending an <code>ImageView</code> and Overriding <code>onMeasure</code> to force the resizing and aspect ratio?</p>
  
  <p>Or is there a clever trick with <code>Matrix</code> to get the job done?</p>