• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Text View print new line \n

Sy Roz

Newbie
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:
String incoming_data;
String StrM;

textView = (TextView) findViewById(R.id.text_print_view);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setSingleLine(false);


@Override
public void onClick(View v) {

    switch (v.getId()) {

 case R.id.btn_send:

                incoming_data = textEdit.getText().toString();    

                StrM = StrM +"\n" + incoming_data ;         
                textView.setText(StrM);


                break;

            case R.id.btn_clear:

                textView.setText("");
                StrM = "";
                break;
 
I don't understand your question. Can you rephrase it please?
 
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.
 
Last edited:
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:
@Override
public void onClick(View v) {

    switch (v.getId()) {
      case R.id.btn_send:
          incoming_data = textEdit.getText().toString();   
          String text = textView.getText();
          text = text + "\n" + incoming_data;
          textView.setText(text);
          break;
          ...
 
Code:
    incoming_data = textEdit.getText().toString();
                String text =  textView.getText().toString();
                text = text + "\n" + incoming_data;
                textView.setText(text);
                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?
 
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.
 
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
 
thanks LV426..

So the efficient way should be like that:

Code:
String   incoming_data = textEdit.getText().toString();
                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?
 
Last edited:
Back
Top Bottom