Home > Enterprise >  why android:breakStrategy not working in TextView?
why android:breakStrategy not working in TextView?

Time:01-11

I have set the breakStrategy to simple and the textview is not behaving properly. This is the result I am getting:

enter image description here

This is the code I used:

<TextView
            android:id="@ id/activity_list_item_notes"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginStart="@dimen/row_top_padding_12"
            android:layout_marginTop="@dimen/margin_xsmall"
            android:layout_marginEnd="@dimen/margin_xsmall"
            android:gravity="center_vertical|start"
            android:visibility="gone"
            tools:visibility="visible"
            android:breakStrategy="simple"
            android:textAlignment="gravity"
            android:textDirection="locale"
            app:layout_constrainedWidth="true"
            app:layout_constraintEnd_toStartOf="@id/amount_and_action_barrier"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintStart_toEndOf="@id/activity_list_item_contact_image"
            app:layout_constraintTop_toBottomOf="@ id/activity_list_item_date"
            tools:targetApi="m" />

I want the text to be displayed in such a way that the first line should fill the whole width of the textview. I mean the word break should start in the first line itself. Doesn't matter if it breaks and splits between both lines. I am testing on device with API level 30.

CodePudding user response:

I think the simplest solution is to replace spaces in your text with non-breaking spaces. In my project I have kotlin extension function for this:

fun String?.useNonBreakingSpace() = this.orEmpty()
    .replace(
        Constants.REGULAR_SPACE_CHARACTER,
        Constants.NON_BREAKABLE_SPACE_UNICODE
    )

and

object Constants {

    ...

    const val REGULAR_SPACE_CHARACTER = ' '
    const val REGULAR_SPACE = REGULAR_SPACE_CHARACTER.toString()
    const val NON_BREAKABLE_SPACE_HTML = "&#160;"
    const val NON_BREAKABLE_SPACE_UNICODE = '\u00A0'

    ...
}
  •  Tags:  
  • Related