开发者

How to delete the old lines of a TextView

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.


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) {
    _debugTextView.append(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 {
                eolIndex++;
            } while(eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n');             
        }
        if (eolIndex < charSequence.length()) {
            _debugTextView.getEditableText().delete(0, eolIndex+1);
        }
        else {
            _debugTextView.setText("");
        }
    }
}

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) {
        errorLog.remove(0);
    }
    updateLog();
}

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


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:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" >

    <TextView
        android:id="@+id/textViewOutput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1000" />
</ScrollView>

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;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _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) {
    _textViewOutput.append(lineText);
    removeLinesFromTextView();
    if(AUTO_SCROLL_BOTTOM)
        _scrollView.post(new Runnable() {
            @Override
            public void run() {
                _scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
}

// 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);
        }
    }
}


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("");


Kotlin answer of Vincent Hiribarren

fun write_terminal_with_limit(data: String?, limit:Int)
{
  log_textView.append(data)
  val nb_line_to_del: Int = log_textView.lineCount - limit
  // Erase excessive lines
  if (nb_line_to_del > 0)
  {
    var end_of_line_idx = -1
    val char_seq: CharSequence = log_textView.text
    for (i in 0 until nb_line_to_del)
    {
      do
      {
        end_of_line_idx++
      }
      while (end_of_line_idx < char_seq.length && char_seq[end_of_line_idx] != '\n')
    }
    if (end_of_line_idx < char_seq.length)
    {
      log_textView.editableText.delete(0, end_of_line_idx + 1)
    }
    else
    {
      log_textView.text = ""
    }
  }
}

I made personnal adjustment...


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);      
textview.setText(str);  


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);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜