1. Download our Official Android App: Forums for Android!

Apps 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
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Feb 29, 2016

    Feb 29, 2016
    10
    1
    16
    Male
    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):
    1.  
    2.  
    3. String incoming_data;
    4. String StrM;
    5.  
    6. textView = (TextView) findViewById(R.id.text_print_view);
    7. textView.setMovementMethod(new ScrollingMovementMethod());
    8. textView.setSingleLine(false);
    9.  
    10.  
    11. @Override
    12. public void onClick(View v) {
    13.  
    14.     switch (v.getId()) {
    15.  
    16.  case R.id.btn_send:
    17.  
    18.                 incoming_data = textEdit.getText().toString();    
    19.  
    20.                 StrM = StrM +"\n" + incoming_data ;        
    21.                 textView.setText(StrM);
    22.  
    23.  
    24.                 break;
    25.  
    26.             case R.id.btn_clear:
    27.  
    28.                 textView.setText("");
    29.                 StrM = "";
    30.                 break;


     

    Advertisement

  2. scary alien

    scary alien not really so scary
    Moderator
    Rank:
     #9
    Points:
    2,138
    Posts:
    22,319
    Joined:
    Mar 5, 2010

    Mar 5, 2010
    22,319
    23,814
    2,138
    Male
    space alien ;)
    Indy
    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.

    Cheers!
     
  3. Sy Roz

    Sy Roz Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Feb 29, 2016

    Feb 29, 2016
    10
    1
    16
    Male
    ok thanks.
     
    scary alien likes this.
  4. Sy Roz

    Sy Roz Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Feb 29, 2016

    Feb 29, 2016
    10
    1
    16
    Male
    Hay guys so many views as more then 200 and what no body know about this? Hard to believe...
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    I don't understand your question. Can you rephrase it please?
     
  6. Sy Roz

    Sy Roz Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Feb 29, 2016

    Feb 29, 2016
    10
    1
    16
    Male
    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
  7. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    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):
    1.  
    2. @Override
    3. public void onClick(View v) {
    4.  
    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.           ...
    13.  
     
    scary alien likes this.
  8. Sy Roz

    Sy Roz Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Feb 29, 2016

    Feb 29, 2016
    10
    1
    16
    Male
    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?
     
  9. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    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.
     
  10. Sy Roz

    Sy Roz Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Feb 29, 2016

    Feb 29, 2016
    10
    1
    16
    Male
    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
     
  11. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    Sy Roz and scary alien like this.
  12. Sy Roz

    Sy Roz Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Feb 29, 2016

    Feb 29, 2016
    10
    1
    16
    Male
    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

Loading...