1. Are you ready for the Galaxy S20? Here is everything we know so far!

Text View print new line \n

Discussion in 'Android Development' started by Sy Roz, Feb 29, 2016.

  1. Sy Roz

    Sy Roz Newbie
    Thread Starter

    Hello to everyone..

    So this is test code to understand what Im tolking about:

    So basically what i need is
    let]s say I have incoming data which is new evry time I grab it then I need to print that In text view fron the new line \n but what ever I do it's reprints the previous incoming data so avoid that I found this way see my code...
    But I pretty sure this is NOT right way because the Text view reprints the StrM string evry time and also which even more to it it's expends every time. Now it's like Text View buffer but Im pretty sure here suppose to be its own Text view buffer. So here the question by the way how big the StrM can be.????..

    So pls for those who know what's the efficiant way of same what's this code do???

    Code (Text):
    3. String incoming_data;
    4. String StrM;
    6. textView = (TextView) findViewById(R.id.text_print_view);
    7. textView.setMovementMethod(new ScrollingMovementMethod());
    8. textView.setSingleLine(false);
    11. @Override
    12. public void onClick(View v) {
    14.     switch (v.getId()) {
    16.  case R.id.btn_send:
    18.                 incoming_data = textEdit.getText().toString();    
    20.                 StrM = StrM +"\n" + incoming_data ;        
    21.                 textView.setText(StrM);
    24.                 break;
    26.             case R.id.btn_clear:
    28.                 textView.setText("");
    29.                 StrM = "";
    30.                 break;


  2. Best Answer:
    Post #11 by Deleted User, Mar 2, 2016 (1 points)
  3. scary alien

    scary alien not really so scary

    Welcome to our AndroidForums, @Sy Roz :).

    I've moved your thread over here to our Application Development area for you where you'll get more targeted views/replies.

  4. Sy Roz

    Sy Roz Newbie
    Thread Starter

    ok thanks.
    scary alien likes this.
  5. Sy Roz

    Sy Roz Newbie
    Thread Starter

    Hay guys so many views as more then 200 and what no body know about this? Hard to believe...
  6. Deleted User

    Deleted User Guest

    I don't understand your question. Can you rephrase it please?
  7. Sy Roz

    Sy Roz Newbie
    Thread Starter

    Look at the code above... What I want is the same is that code does. But I just made it from my head which I don't think is right way. I want the TextView hold the content it's already got and show it. And every time the incoming data it's just one more string i want print in new line. You see now the code above is appending the StrM with new incoming data string and then the TextView reprints evry time entire StrM. What I do thing is not right way is because the StrM always grows so how big it can be and how fast the textView can reprint the StrM if it's big enough ? I think it must be stored somewhere somehow in the textview as shadow on background and not reprinting the StrM evry time ... But whatever I do this the only why I found pretty close what I need.
    #6 Sy Roz, Mar 2, 2016
    Last edited: Mar 2, 2016
  8. Deleted User

    Deleted User Guest

    Why do you need to store the textView String in a variable? Just get the current value of the textView and append the new content -

    Code (Text):
    2. @Override
    3. public void onClick(View v) {
    5.     switch (v.getId()) {
    6.       case R.id.btn_send:
    7.           incoming_data = textEdit.getText().toString();  
    8.           String text = textView.getText();
    9.           text = text + "\n" + incoming_data;
    10.           textView.setText(text);
    11.           break;
    12.           ...
    scary alien likes this.
  9. Sy Roz

    Sy Roz Newbie
    Thread Starter

    Code (Text):
    1.     incoming_data = textEdit.getText().toString();
    2.                 String text =  textView.getText().toString();
    3.                 text = text + "\n" + incoming_data;
    4.                 textView.setText(text);
    5.                 break;

    ok now whatever the TextView have it it dumps it in String text pretty much same accept if we initialize it right there in the spot so basiclly I should not worry now about the growing string like how big it can be?
    But still now we dump all what textView have to a text string then append incoming data then reprint the intire thing again... What if I need sense kinda a lot of information evry half a second for a wile? Obviously the content of text view will grow will it slow the app because of reprinting to much information? Is this the right way?
  10. Deleted User

    Deleted User Guest

    How big can it be? Depends on the size of heap memory, which varies between devices. Typically 32MB.
    But seriously, if your String is consuming this amount of memory you are using the wrong approach.
  11. Sy Roz

    Sy Roz Newbie
    Thread Starter

    Ok So basically the TextView can hold up to 32MB at ones. that's sure plenty, that's good if understand it right.... But how fast it can reprint that entire 32? I don't wanna bullied the test here to check :)), just curious ? one

    But maybe here is the way the TextView still can hold the content and showing it but not reprints entire itself? That's what it does now, right? What if we just add to a content new incoming data? or it has to be reprinting itself evry time ??? and this the right way that's how we do it and we done with it. two
  12. Deleted User

    Deleted User Guest

    Sy Roz and scary alien like this.
  13. Sy Roz

    Sy Roz Newbie
    Thread Starter

    thanks LV426..

    So the efficient way should be like that:

    Code (Text):
    1. String   incoming_data = textEdit.getText().toString();
    2.                 textView.append(incoming_data +"\n");
    Also guys can you kick me to the right direction. How to set the hold the focus on incoming data in TextView?
    #12 Sy Roz, Mar 2, 2016
    Last edited: Mar 3, 2016

Share This Page