Home » Android » android – How to delete the old lines of a TextView

android – How to delete the old lines of a TextView

Posted by: admin June 15, 2020 Leave a comment


I’m developping an app which constantly needs to show the results to the user in a TextView like some sort of log.

The app works nicely and it shows the results in the TextView but as long as it keeps running and adding lines the app gets slower and crashes because of the character length of the TextView.

I would like to know if the android API provides any way to force a TexView to automatically delete the oldest lines that were introduced in order to make room for the new ones.

How to&Answers:

I had the same problem. I just resolved it.

The trick is to use the getEditableText() method of TextView. It has a replace() method, even a delete() one. As you append lines in it, the TextView is already marked as “editable”, which is needed to use getEditableText(). I have something like that:

private final static int MAX_LINE = 50;
private TextView _debugTextView; // Of course, must be filled with your TextView

public void writeTerminal(String data) {
    // Erase excessive lines
    int excessLineNumber = _debugTextView.getLineCount() - MAX_LINE;
    if (excessLineNumber > 0) {
        int eolIndex = -1;
        CharSequence charSequence = _debugTextView.getText();
        for(int i=0; i<excessLineNumber; i++) {
            do {
            } while(eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n');             
        if (eolIndex < charSequence.length()) {
            _debugTextView.getEditableText().delete(0, eolIndex+1);
        else {

The thing is, TextView.getLineCount() returns the number of wrapped lines, and not the number of “\n” in the text… It is why I clear the whole text if I reach the end of the text while seeking the lines to delete.

You can do that differently by erasing a number of characters instead of erasing a number of lines.


This solution keeps track of the log lines in a list and overwrites the textview with the contents of the list on each change.

private List<String> errorLog = new ArrayList<String>();
private static final int MAX_ERROR_LINES = 70;
private TextView logTextView;

public void addToLog(String str) {
    if (str.length() > 0) {
        errorLog.add( str) ;
    // remove the first line if log is too large
    if (errorLog.size() >= MAX_ERROR_LINES) {

private void updateLog() {
    String log = "";
    for (String str : errorLog) {
        log += str + "\n";


The TextView shows what you set via setText() method. So this sounds to me like you should cut down the input you provide.

To empty the TextView, you can do setText("");


Here is an example that adds lines to an output log limited by the set max lines. The scrollview will auto scroll to the bottom after every line is added. This example work purely with the contents of the TextView so it doesn’t have the need for a separate data collection.

Add the following to your activity xml:

    android:scrollbars="vertical" >

        android:maxLines="1000" />

In your activity add the following code:

private static final int MAX_OUTPUT_LINES = 50;
private static final boolean AUTO_SCROLL_BOTTOM = true;

private TextView _textViewOutput;

protected void onCreate(Bundle savedInstanceState) {
    _textViewOutput = (TextView) findViewById(R.id.textViewOutput);

//call to add line(s) to TextView
//This should work if either lineText contains multiple
//linefeeds or none at all
private void addLinesToTextView(String lineText) {
        _scrollView.post(new Runnable() {
            public void run() {

// remove leading lines from beginning of the output view
private void removeLinesFromTextView() {
    int linesToRemove = _textViewOutput.getLineCount() - MAX_OUTPUT_LINES;
    if (linesToRemove > 0) {
    for (int i = 0; i < linesToRemove; i++) {
        Editable text = _textViewOutput.getEditableText();
            int lineStart = _textViewOutput.getLayout().getLineStart(0);
            int lineEnd = _textViewOutput.getLayout().getLineEnd(0);
            text.delete(lineStart, lineEnd);


I think you are using TextView.append(string) then it will add to old text.
If you are setting using setText it will replace the old text


This is an old one, but I just found looking for a solution to my own problem.

I was able to remove all TextViews from a LinearLayout using nameoflayout.removeAllViews();

There is another method that will allow you to remove views from specified places in the layout using ints, it’s: nameoflayout.removeViews(start, count); so I’m sure you could create a time out for how long textviews remain visible.


No, android API doesn’t provide any functionally that delete oldest lines from textview automatically till API level 25. you need to do it logically.


Try to write a function that takes an old string on TextView and add new string to it, then get substring last strings that TextView capable. And set it to TextView. Something like this:

String str = textview.getText();      
str += newstring;   
int ln = str.length();   
ln = ln-250;  
if (ln<0) ln=0;     
str = str.substring(ln);      


reference Vincent Hiribarren answer.
make it simple–>

TextView _debugTextView;
//if excess 20 lines keep new 200 chars
if(_debugTextView.getLineCount() >20) _debugTextView.getEditableText().delete(0,_debugTextView.getText().length()-200);