android – Unexpected line break when using spannable-ThrowExceptions

Exception or error:

My application is the arabic dictionary. I have the following TextView

<TextView
        android:id="@+id/txtArInf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:textColor="@color/primaryText"
        android:textIsSelectable="true"
        android:textSize="34sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="إِتْيَانٌ" />

and the method for highlighting searching matches:

private fun highlightArabicQuery(resources: Resources, spannable: Spannable, query: Pattern) {
    val matcher = query.matcher(spannable)
    val color = resources.getColor(R.color.background_match_highlight)
    while (matcher.find()) {
        spannable.setSpan(
                BackgroundColorSpan(color),
                matcher.start(),
                matcher.end(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    }
}

I get the unexpected line breaks in some words, which is look like this.

Unexpected line break when using spannable
Unexpected line break when using spannable 2

The regex pattern in the example above is ن[\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652\u0653\u0670]*ص[\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652\u0653\u0670]*ر[\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652\u0653\u0670]* (Unicode symbols in brakets are arabic diacritics)

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *