Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView breaks my word by letters

My requirements: create "incoming bubble" with width by content and max width 90%.

I have this markup:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0"
    tools:background="@color/white_smoke">

    <LinearLayout
        android:id="@+id/flBubble"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:background="@drawable/bubble_in"
        android:layout_weight="0.9">

        <ImageView
            android:id="@+id/ivSay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:contentDescription="@string/default_content_description"
            android:padding="8dp"
            android:src="@drawable/ic_play_circle_outline_black_24dp"
            android:tint="@color/primary"/>

        <TextView
            android:id="@+id/tvValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="8dp"
            android:textColor="@color/black"
            android:textSize="16sp"
            tools:text="I would like to go to an Italian restaurant"/>
    </LinearLayout>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.1"/>
</LinearLayout>

Sometimes I get the following result: bad word wrapping

But I expect the following result (it's falsely encouraging screenshot from Android Studio preview): expected word wrapping

How can I prevent breaking word restaraunt by letters?

UPDATE

Although I use minSdk=15 I tried to use breakStrategy and I haven't get expected result. android:breakStrategy="simple": simple break strategy

android:breakStrategy="balanced": balanced break strategy

I found a related question: Force next word to a new line if the word is too long for the textview, but I didn't undestand how can I get maximum available width for TextView with layout_width="wrap_content?

It would be great if I could override the TextView.setText and place line breaks there if needed.

like image 658
Dem0n13 Avatar asked Aug 16 '17 18:08

Dem0n13


2 Answers

OMG, there were &nbsp; in my string!

value.replaceAll("\\s", " ");

Thank you all!

like image 187
Dem0n13 Avatar answered Sep 29 '22 09:09

Dem0n13


Use MaxWidth property for textview or else you should provide width for textview

   <com.custom.views.CustomTextView
        android:id="@+id/txt_send_chat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:maxWidth="250dp"
        android:textColor="@color/color_chat_sender"
        android:textSize="16sp"
        app:font_name="@string/font_roboto_regular" />
like image 22
Keerthivasan Avatar answered Sep 29 '22 07:09

Keerthivasan