TextRendering in TextView and EditText

291 views Asked by At

I have a simple layout like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/txtContent2"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:background="#fed9f4"
        android:textSize="22sp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"/>

    <TextView
        android:id="@+id/txtBelow"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:background="#fed9f4"
        android:textSize="22sp"/>
</LinearLayout>

A TextView and an EditText. When I set a same text in both of them, it seems that each one renders text differently. like below: Top view is EditText and below is TextView

I'm using StaticLayout to measure text and identify text bounds in each line and I have to set text to a TextView(so user can't edit or select it).

But it seems StaticLayout text boundaries calculations match with EditText not TextView.

StaticLayout layout = new StaticLayout(content, txtContent.getPaint(),
            txtContent.getWidth(),
            Layout.Alignment.ALIGN_NORMAL, 1, lineSpace, false);

My question is why text rendering is different in TextView and EditText and how I can measure text with StaticLayout and set text to TextView so that each line's start and end offsets matches exactly with result that user see after setText

1

There are 1 answers

0
Siyamed On

Try to set the same breakStrategy and https://developer.android.com/reference/android/widget/TextView.html#attr_android:hyphenationFrequencyenter link description here for both of the views, since they might be different for EditText and TextView defaults.