title: Android: How to stretch an image to the screen width while maintaining aspect ratio?
body: <p>I want to download an image (of unknown size, but which is always roughly square) and display it so that it fills the screen horizontally, and stretches vertically to maintain the aspect ratio of the image, on any screen size. Here is my (non-working) code. It stretches the image horizontally, but not vertically, so it is squashed...</p>
  
  <pre><code>ImageView mainImageView = new ImageView(context);
      mainImageView.setImageBitmap(mainImage); //downloaded from server
      mainImageView.setScaleType(ScaleType.FIT_XY);
      //mainImageView.setAdjustViewBounds(true); 
      //with this line enabled, just scales image down
      addView(mainImageView,new LinearLayout.LayoutParams( 
              LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  </code></pre>