title: Truly top-aligning text in Android TextView
body: <p>I am trying to display a <a href="http://developer.android.com/reference/android/widget/TextView.html" rel="nofollow noreferrer">TextView</a> in Android such that the text in the view is top-aligned:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // Create container layout
      FrameLayout layout = new FrameLayout(this);
  
      // Create text label
      TextView label = new TextView(this);
      label.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25);     // 25 pixels tall
      label.setGravity(Gravity.TOP + Gravity.CENTER);        // Align text top-center
      label.setPadding(0, 0, 0, 0);                          // No padding
      Rect bounds = new Rect();
      label.getPaint().getTextBounds("gdyl!", 0, 5, bounds); // Measure height
      label.setText("good day, world! "+bounds.top+" to "+bounds.bottom);
      label.setTextColor      (0xFF000000);                  // Black text
      label.setBackgroundColor(0xFF00FFFF);                  // Blue background
  
      // Position text label
      FrameLayout.LayoutParams layoutParams = 
              new FrameLayout.LayoutParams(300, 25, Gravity.LEFT + Gravity.TOP);
                                                              // also 25 pixels tall
      layoutParams.setMargins(50, 50, 0, 0);
      label.setLayoutParams(layoutParams);
  
      // Compose screen
      layout.addView(label);
      setContentView(layout);
  }
  </code></pre>
  
  <p>This code outputs the following image:</p>
  
  <p><img src="https://i.stack.imgur.com/p7UBN.png" alt="enter image description here"></p>
  
  <p>The things to note:</p>
  
  <ul>
  <li>The blue box is 25 pixels tall, just like requested</li>
  <li>The text bounds are also reported as 25 pixels tall as requested (6 - (-19) = 25)</li>
  <li>The text does not start at the top of the label, but has some padding above it, ignoring setPadding()</li>
  <li>This leads to the text being clipped at the bottom, even though the box technically is tall enough</li>
  </ul>
  
  <p>How do I tell the TextView to start the text at the very top of the box?</p>
  
  <p>I have two restrictions to possible answers:</p>
  
  <ul>
  <li>I do need to keep the text top-aligned, though, so if there is some trick with bottom-aligning or centering it vertically instead, I can't use it, since I have scenarios where the TextView is taller than it needs to be.</li>
  <li>I'm a bit of a compatibility-freak, so if possible I'd like to stick to calls that were available in the early Android APIs (preferably 1, but definitely no higher than 7).</li>
  </ul>