Line break without increasing text length

I can break the string using the following code:

String str1 = "TEST1"; // length = 5 String str2 = "TEST2"; // length = 5 TextView textView = (TextView)findViewById( R.id.text_view ); textView.setText(str1 + '\n' + str2); 

But the final length of the text is 11.

Question:

Is there any special character or method that will allow me to achieve the same result inside my TextView without increasing the length of the text?

What I'm trying to achieve:

I have a data format that is stored in JSON. He looks like

 [{type: line, params: {line params}}, {type: text, params: {text params}, ...] 
  • There is always a line at the beginning

  • Each paragraph begins with a line (therefore, it acts as a line separator, which is stored at the beginning of the line, not at the end)

  • The size of each line is 1, that is, each line is considered a single character

  • Each paragraph ends with the text of the last character (not '\ n')

  • There are some line parameters (e.g. BulletList, Numeric list, Paragraph)

I need a strict comparison between my TextView and the source data, i.e. for each cursor position in my TextView I need to calculate how many characters precede the original data.

+4
source share
4 answers

On your question, my answer will be no. But you can make your own TextView and change how it calculates the length of the text, for example, ignoring "/ n" when counting the length.

-1
source

Take two text elements and add them below the other. Then you will not find a problem with the length.

  like : <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView1" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:text="TextView2" /> </RelativeLayout> 
0
source
 String str1 = "TEST1"; String str2 = "TEST2"; TextView text=(TextView)vi.findViewById(R.id.text); text.setText(str1); text.append(Html.fromHtml(< br>)); text.append(str2); 

Hope it works :)

0
source

Well, there is a difficult way

  String str1 = "TEST1"; // length = 5 String str2 = "TEST2"; // length = 5 textView = (TextView)findViewById( R.id.textView1 ); textView.setWidth(120); textView.setTextSize(20); textView.setText(str1 + str2); //textView.getText().toString().length() length = 10 

in xml

 <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="TextView" /> 
-1
source

All Articles