Preventing text breaks in TextView

I have a fixed width TextView as follows:

 <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="12sp" tools:text="Health Department" android:layout_marginTop="4dp" android:maxLines="2" /> 

... But it looks like this:

  |Health Dep-| | artment | 

... Although I want it to be:

  | Health | |Department| 

What XML attribute can I use for this?

+5
source share
4 answers

From api 23 you can choose a break strategy and this should solve your problem.

By default, android for TextView uses BREAK_STRATEGY_HIGH_QUALITY , and this causes a word violation using a hyphen.

If you want to avoid this, you can set the gap strategy BREAK_STRATEGY_SIMPLE .

Further information can be found in the api doc .

I hope this helps you.

+6
source

This is a new thing in android version 6 (Marshmallow).

Try adding code below to TextView in xml layout

 android:hyphenationFrequency="none" 
+4
source

I wanted to apply the fracture strategy mentioned in this answer to all my text views throughout the application and added a new v23 / styles.xml file containing:

 <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="Base.Widget.TextView" parent="android:Widget.TextView"> <item name="android:breakStrategy">simple</item> </style> </resources> 

And I added the following to my themes.xml

 <item name="android:textViewStyle">@style/Base.Widget.TextView</item> 
+3
source

Try using this in a textview, this works for me

 android:breakStrategy="simple" 
+3
source

Source: https://habr.com/ru/post/1260469/


All Articles