Nikhil Kumar Nikhil Kumar - 7 months ago 28
Java Question

show textview in next line

TextView textView =(TextView)findViewById(R.id.text);

for(int i=1;i<6;i++)

{

textView.setText(i);

textView.setText("\n");

}


This is not printing

1

2

3

4

5

Please help me how to print the above output

Answer

whenever you call setText on a TextView you change the entire text. you should create your String first and then set that as your TextView's text.

String text = "";
for(int i=1;i<6;i++) {
  text += i+"\n";
}
textView.setText(text)'

another solution is using append instead of setText

for(int i=1;i<6;i++) {
  textView.append(i+"\n");
}
Comments