Tuesday, April 12, 2016

Prevent line-break in TextView

Leave a Comment

In my Android app I have a text view that displays text containing special characters. The TextView somehow automatically breaks strings at the characters '/' and '-'.

For example, the string "aaaaaaa/bbb-ccccc/ddd" is displayed as

aaaaaaa/ bbb- ccccc/ ddd 

However, I would like to display it without any linebreaks except the one at the boundaries of the view, i.e., like this:

aaaaaaa/bb bb-ccccc/d dd 

Is there any way to deactivate the automatic line-breaks or to escape these characters? I already tried escaping with \uFEFF without success.

6 Answers

Answers 1

Keep your textview attribute

android:layout_width="wrap_content" android:layout_height="wrap_content" 

Define Your string in string.xml

<string name="Username"> aaaaaaa\/bb\nbb\-ccccc\/d\ndd</string> 

Answers 2

Maybe this is a solution: http://stackoverflow.com/a/22337074/3472905

I've added the slash as mentioned:

public class WordBreakTransformationMethod extends ReplacementTransformationMethod {     private static WordBreakTransformationMethod instance;      private WordBreakTransformationMethod() {}      public static WordBreakTransformationMethod getInstance() {         if (instance == null) {             instance = new WordBreakTransformationMethod();         }          return instance;     }      private static char[] dash = new char[]{'-', '\u2011'};     private static char[] space = new char[]{' ', '\u00A0'};     private static char[] slash = new char[]{'/', '\u2215'};      private static char[] original = new char[]{dash[0], space[0], slash[0]};     private static char[] replacement = new char[]{dash[1], space[1], slash[1]};      @Override     protected char[] getOriginal() {         return original;     }      @Override     protected char[] getReplacement() {         return replacement;     } } 

Answers 3

There no ready solution and no such thing as "wrap text by letters in TextView" the only way to do it in a good way is to extend TextView and modify Paint's breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth) function.

Also, you can calculate TextView size in pixels, calculate width of one letter in pixels, then find number of letters (X) that will fit in one line and then insert linebreak after each X letters

Answers 4

you probably can use the Lines attribute or its counter-part method setLines(int)

Answers 5

I have tested the following code. You can even convert it into a function:

    String specialString = "a/b/-c/d-d";     String[] specialArray = specialString.split("/");     String str = "";     for(int i = 0; i < specialArray.length - 1; i++){         str = str + specialArray[i] + Character.toString((char) 47);     }     str = str + specialArray[specialArray.length - 1];     specialArray = str.split("-");     str = "";     for(int i = 0; i < specialArray.length - 1; i++){         str = str + specialArray[i] + Character.toString((char) 45);     }     str = str + specialArray[specialArray.length - 1];     textView.setText(str); 

Now the text does not escape

Answers 6

You Can Try this : "aaaaaaa"+"/"+"bbb-ccccc"+"/"+"ddd"

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment