0

Even when gravity is set to top the text has some padding between it and view's border. I need to text to completely touch the border.

Bellow you can see the gap I'm trying to get rid of.

Text not touching view's border

And here is the layout used:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/someView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here is some text"
        android:textSize="30dp"
        android:gravity="top" />
</LinearLayout>
Alex.F
  • 5,648
  • 3
  • 39
  • 63
  • what is the built in padding??? I dont know about that. i think we can force the text to be in a direction by using android:gravity attribute of the TextView – Abdul Mohsin Nov 11 '14 at 14:08
  • What exactly are you trying to do? This is a pretty vague statement, are you trying to get it to align to the left or right or something? Based on your question I would say use margin or padding, but apparently that isn't what you need... – zgc7009 Nov 11 '14 at 14:21
  • Thank you for your input I edited the question so it's more clear (I hope). @zgc7009 – Alex.F Nov 11 '14 at 15:07
  • android:includeFontPadding="false" in your TextView. Sorry I just misunderstood your question at first, the image did it for me :P – zgc7009 Nov 11 '14 at 15:12
  • @zgc7009, tried it already and unfortunately it will still leave a gap – Alex.F Nov 11 '14 at 15:38
  • reference the answer:[How to remove the top and bottom space on textview of Android](http://stackoverflow.com/a/40072552/703225) – qinmiao Oct 16 '16 at 16:40

3 Answers3

3

Try to create 9.patch line and set it to your TextView: android:drawableTop="@drawable/your_line". Then set android:drawablePadding="-10dp"

This trick must work perfectly.

QArea
  • 4,955
  • 1
  • 12
  • 22
  • Thanks your solution is the **only** one that works. No need to use 9.patch though, a simple `color` will do fine as well. I'll edit your answer to show the layout. – Alex.F Nov 18 '14 at 08:03
1

Try this on TextView in your xml file :

android:includeFontPadding="false"

--

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/someView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here is some text"
        android:textSize="30dp"
        android:includeFontPadding="false"
        android:gravity="top" />
</LinearLayout>
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • This single line worked. I can't believe I'm the first upvote on this answer since it was created :) – kurt Mar 26 '17 at 14:09
0

In xml try, android:paddingTop="-5dp"

Negative paddings always seem to work for me.

Set the height manually instead of wrapping the content.

The linear layout is the item with the padding. You need to modify that element as well to remove the space.

Lukos
  • 712
  • 4
  • 5