Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So the space at the top of the TextView is padding used for characters outside the English language such as accents. To remove this space you can either set the <code>android:includeFontPadding</code> attribute to <code>false</code> in your XML or you can do it programmatically with the function <code>setIncludeFontPadding(false)</code>. </p> <p>Look at the <a href="http://developer.android.com/reference/android/widget/TextView.html" rel="noreferrer">SDK documentation for TextView</a> if this is still unclear. </p> <p><strong>EDITED ANSWER</strong><br/> If setting the <code>android:includeFontPadding</code> attribute does not accomplish what you're trying to do, the other solution is to override the <code>onDraw(Canvas canvas)</code> method of the TextView that you're using so that it eliminates the additional top padding that Android adds to every TextView. After writing my original answer, I noticed that for some reason TextView includes extra padding in addition to the font padding. Removing the font padding <em>as well</em> as this additional padding perfectly aligns the text to the top of the TextView. Look at the code snippet below. </p> <pre><code>public class TopAlignedTextView extends TextView { // Default constructor when inflating from XML file public TopAlignedTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } // Default constructor override public TopAlignedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs); setIncludeFontPadding(false); //remove the font padding setGravity(getGravity() | Gravity.TOP); //make sure that the gravity is set to the top } /*This is where the magic happens*/ @Override protected void onDraw(Canvas canvas){ TextPaint textPaint = getPaint(); textPaint.setColor(getCurrentTextColor()); textPaint.drawableState = getDrawableState(); canvas.save(); //converts 5dip into pixels int additionalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics()); //subtracts the additional padding from the top of the canvas that textview draws to in order to align it with the top. canvas.translate(0, -additionalPadding); if(getLayout() != null) getLayout().draw(canvas); canvas.restore(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload