Question

i want to make new line on a textview each time button clicked from input on edittext. But textview only update the first line and not showing multiple lines. here's what I've done

1) using ordinary line break \r\n

enterButton.setOnClickListener(new OnClickListener(){


            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                resultTextView.setText(inputEditText.getText()+"\r\n");
                inputEditText.setText("");
            }

        });

2) using line separator

 enterButton.setOnClickListener(new OnClickListener(){


            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                resultTextView.setText(inputEditText.getText()+ System.getProperty("line.separator"));
                inputEditText.setText("");
            }

        });

Textview's xml

<TextView
        android:id="@+id/resultTextView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/dataLogButton"
        android:layout_alignLeft="@+id/tableRow2"
        android:layout_below="@+id/tableRow2"
        android:layout_marginTop="25dp"
        android:maxLines="20"
        android:singleLine="false" />

my question is why?why would no line break? and how do i fix this? thanks

Was it helpful?

Solution

On each click, you are replacing the text in 'resultTextView' with what is in 'inputEditText', albeit with a linebreak on the end. You are not adding to what was there before.

I think this is what you are after:

resultTextView.setText(resultTextView.getText() + "\r\n" + inputEditText.getText());

So you set it to what it currently is, plus a new line break, plus the new line from the edittext.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top