Problems with Kerning in Android Textview

804 Views Asked by At

I have an interesting thing (bug?) that i have run into while working with the TextView in Android.

I have a textview with the following attributes (clipped for brevity):

<TextView
    android::id="@+id/myId"
    android:text="Some Temporary Text 1234"
    android:textSize="7sp"
    android:typeface="monospace"
    ...
/>

This displays as expected except the kerning is different from a textview that sets these properties like this:

TextView textView = (TextView) xxx.findViewByID(R.id.myId);
textView.setTextSize(7);
textView.setTypeface(Typeface.MONOSPACE);

I was beating my head against a wall trying to figure out why when moving from building the view in code to XML the output was different. It was hard to tell what was going on because I only had the one device attached.

So, I took a snapshot of the device with each method and used Photoshop to overlay the text onto each other. The text and font are identical (i.e. I can line up letters pixel for pixel) but the kerning (the space between the letters) was different!

For that textview only, I switched to setting it in code instead of XML and compared again....and they are identical.

Here is a comparison of the two outputs. The top is done in XML. The bottom is done in code. enter image description here

Edit: Please note, the missing "INT" in the picture is just due to clipping of the picture when I captured the example. The text strings are the same and the "INT" is present....just off the picture.

So there is definitely a difference there. But notice the actual characters are the same.

Am I missing something in the XML? I have searched for options surrounding kerning but aside form installing a custom font (which I don't want to do), I don't see a way to adjust the kerning in android. Is there a way? Is this a bug in android?

I can get around the issue but I was trying to eliminate the code as it is really out of place.

0

There are 0 best solutions below