0

I tested emulators and real devices but as you can see there is no change between 40 dp and 40.2 dp! I'm working on a project with a very high font sensitivity and the issue is behaviour of android text engine.

Is there any way to prevent that?

Please help!

This is the code:

<?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:text="SAMPLE TEXT"
            android:textSize="40dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SAMPLE TEXT"
            android:textSize="40.2dp" />

    </LinearLayout>

The result:

seyfullah.bilgin
  • 1,421
  • 14
  • 16

2 Answers2

0

You should always use SP for fonts as it respects the user preferences

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SAMPLE TEXT"
        android:textSize="40sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SAMPLE TEXT"
        android:textSize="40.2sp" />
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0
<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:text="SAMPLE TEXT"
    android:textSize="40sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SAMPLE TEXT"
    android:textSize="40.2sp" />

you can see the difference like this(in android studio you need to zoom the layout screen).There is no behavior issue of android text engine.

enter image description here

  • The issue is behaviour of android text engine absulately. I tried dp, sp, px. I noticed that when the font size is specified with setTextSize method in android textview, the decimals of the font size were determined based on resolution and I realized this by using getTextSize method. There is a kind of rounding mechanism. See also http://imgur.com/a/JpEps – seyfullah.bilgin Jul 18 '17 at 10:26