How to add a line in Android TextView?

There is TextView.append (), but this adds text to the end of the TextView. I want what I add at the beginning (i.e. Appear at the top of the TextView window).

+6
source share
4 answers

Have you tried this

textview.setText(" append string" + textView.getText()); 

Although this mileage will be lost using this method.

+8
source

If you are worried about spannables, you can use something like this:

 textView.getEditableText().insert(0, "string to prepend"); 
+7
source

GetEditableText () returns null for me. Instead, to add text to the spannable, I set the buffer buffer type to SPANNABLE: in the code:

 myTextView.setText(myText, TextView.BufferType.SPANNABLE); 

or using the xml property android: bufferType in your TextView.

Then drag getText () to Spannable, after which I finish the additional text with the existing text:

 Spannable currentText = (Spannable) tvTitle.getText(); CharSequence indexedText = TextUtils.concat(String.format("%d. ", index), currentText); myTextView.setText(indexedText); 

As far as I can tell, all functions are available from API level 1.

+3
source

Then use the line you want to add "hello" to the text box as

 textview.setText("hello"+textView.getText()) 
+2
source

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


All Articles