4

I am using custom fonts in my android TextView.

Typeface tf = Typeface.createFromAsset(getAssets(),
                "nunitomedium.ttf");
        txt2= (TextView) findViewById(R.id.txt2);
        txt2.setTypeface(tf);

But padding between lines are not properly show.

<TextView
        android:id="@+id/txt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_15sdp"
        android:text="You can send emergency blast to friends and family whenever you feel uncomfortable. Once activated, you will be able to send an emergency blast to your safety network within seconds with the pressing of one button."
        android:textColor="@color/colorBlack"
        android:textSize="@dimen/_15sdp" />

Here is my output :

enter image description here

I also tried with following code :

android:includeFontPadding="false"

but still same issue. Not sure but I also tried to use justifyTextView from Here but same issue. Does anybody faced same issue before?

Thanks in advance.

EDIT :

If i use android:lineSpacingExtra 1st line have more space then other lines. Here is my code and output.

<TextView
        android:id="@+id/txt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_15sdp"
        android:lineSpacingExtra="@dimen/_5sdp" <<<<<<<
        android:text="You can send emergency blast to friends and family whenever you feel uncomfortable. Once activated, you will be able to send an emergency blast to your safety network within seconds with the pressing of one button."
        android:textColor="@color/colorBlack"
        android:textSize="@dimen/_15sdp" />

enter image description here

Community
  • 1
  • 1
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113

3 Answers3

2

Late to the party on this - we had exactly the same issue, was fixed completely by replacing the .ttf font file with the latest from Google Fonts - there's a bad early version of this font doing the rounds. Don't forget to remove any android:lineSpacingMultiplier fudges you've put in place int he meanwhile.

Fisk Debug
  • 156
  • 1
  • 5
  • Hey @Fisk thanks for reply. Can you share font file as well if possible? Thanks! – Hardik Joshi Oct 13 '17 at 16:04
  • 1
    Thank you so much for help @Fisk. You did it. I was working on this issue from past. I keep it with default fonts. but now i am also using nunito fonts in my app. Thank you brother. Happy codding. :) – Hardik Joshi Oct 14 '17 at 17:13
0

Add linespacingMultiplier in your XML

 <TextView
        android:id="@+id/textview_font"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_15sdp"
        android:text="You can send emergency blast to friends and family whenever you feel uncomfortable. Once activated, you will be able to send an emergency blast to your safety network within seconds with the pressing of one button."
        android:textColor="@android:color/black"
        android:lineSpacingMultiplier="3"
        android:textSize="@dimen/_15sdp" />

Or

Add these line in your code:-

 mTextViewFont=(TextView)findViewById(R.id.textview_font);
        Typeface tf = Typeface.createFromAsset(getAssets(),
                "nunitomedium.ttf");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mTextViewFont.setLineSpacing((float) 5.5,(float) 1.2);
        }
        mTextViewFont.setTypeface(tf);
Ankita
  • 1,129
  • 1
  • 8
  • 15
  • still the first line spacing is much larger that all other lines. Themes as a bug in a font itself – Vladyslav Matviienko May 19 '17 at 06:14
  • Agree with @VladMatvienko still 1st line has spacing much larger than all other lines – Hardik Joshi May 19 '17 at 06:17
  • There was issue in the font file that is because of the site from where you downloaded it. I download the file from http://www.dafontfree.net/download-nunito-f92263.htm and then replace your file from asset folder. @HardikJoshi – Ankita May 19 '17 at 07:02
  • I have shared the answer with the output(screenshot) @HardikJoshi – Ankita May 19 '17 at 07:14
0

There was issue in the font file that is because of the site from where you downloaded it. I download the file from

dafontfree.net/download-nunito-f92263.htm

and then replace your file from asset folder.

and this is the output of it.

enter image description here

Ankita
  • 1,129
  • 1
  • 8
  • 15
  • Ok Let me check and will let you know. – Hardik Joshi May 19 '17 at 08:08
  • Hello @Ankita, unfortunately this is not working in my end. Are you using emulator? OR This same output display in real device? – Hardik Joshi May 19 '17 at 08:56
  • I am using emulator. I run your code on the same emulator but it was not showing properly. But when i replce the ttf file it is working fine. @HardikJoshi – Ankita May 19 '17 at 08:57
  • Please check my code. Is it same? https://www.pastiebin.com/591eb3a208cb2 – Hardik Joshi May 19 '17 at 08:58
  • Did You download the ttf file from the link which i given to you? – Ankita May 19 '17 at 09:00
  • If you are using the ttf file from the new link. Then you don't need to change your code(XML or java class). It will work fine after replacing the ttf file from asset folder. @HardikJoshi – Ankita May 19 '17 at 09:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144636/discussion-between-ankita-and-hardik-joshi). – Ankita May 19 '17 at 09:03