-2

This layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="90sp"
        android:text="5000"
        android:id="@+id/textView"
        android:layout_gravity="center"
        />
</LinearLayout>

produces the following:

enter image description here

I want to get rid of the top and bottom margin. Where are they coming from in the first place? Maybe for certain letters in certain fonts/languages.

There is already a thread for this question, but none of the answers really work in all situations or the solutions are hacks. There has to be a simple way.

First idea from the thread: android:includeFontPadding="true" (does not change anyting) Second idea from the thread: android:height="90sp" (removes at least bottom margin)

So, I changed my layout regarding the ideas:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="90sp"
        android:text="5000"
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:includeFontPadding="true" <!-- idea 1 -->
        android:height="90sp" <!-- idea 2 -->
     />
</LinearLayout>

Still it procudes a top margin:

enter image description here

Third idea from the thread: android:layout_marginTop="-12sp" If add this property it looks at least as desired. But -12sp seems so arbitrary to me. I just adjusted this value via trial and error and I don't like that.

This does not seem like a clean solution to me. This is such a simple thing, I just cannot believe how Android can make it so hard, that you have to use hacks.

Is there a clean solution out there?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • Have to tried removing `android:height="90sp"` property of textview ? – Hitesh Sahu Jul 07 '16 at 10:10
  • @HiteshSahu Yes, I have! Like I said this is a part of the top answer from the thread I linked. It at least removed the bottom margin. – Willi Mentzel Jul 07 '16 at 10:11
  • This is possible, but it will require a custom `View` class which adjusts its contents. That seemingly extraneous padding is just due to how Android lays out and handles drawing text. There's not really anything you can do in XML to remedy that, other than the hacks you've already described. – Mike M. Jul 07 '16 at 10:24
  • @MikeM. this (margin) is really ugly especially if I only want to output a number. Custom view class.. ok, I think I know how I would do that, but would have never thought such measures are necessary for something so simple. – Willi Mentzel Jul 07 '16 at 10:29
  • 1
    Yeah, this has always been an issue. Most recommendations you'll see here are an ugly margin/padding hack that don't really work, and are just best guesses as to the values. The correct way is to measure the `View`s text bounds. After that, it kinda depends on what you need. If you always need an _exact_ font size, then you can adjust the `View`s paddings, but that can get a little hairy. If the main concern is just for the text to go all the way to the edge, then scaling the contents up is a little better. I had to write a custom `View` like this awhile ago. I'll see if I can dig it up later. – Mike M. Jul 07 '16 at 10:37

2 Answers2

-1

Use the below syntax to set text size programmatically and set the required size in dimes file.

textView.setTextSize(getResources().getDimension(R.dimen.textsize));

To set padding use below code based on your needs

yourTextView.setPadding(0, 10, 0, 0);
raasesh
  • 161
  • 11
-1

Set Linearlayout and TextView padding and margins to 0dp. Should work.

Add those to both views:

android:padding="0dp"
android:layout_margin="0dp"
user6547359
  • 204
  • 1
  • 9