0
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

Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36

3 Answers3

1

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");
}
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
0

I want from you in one comment to please show what it is printing right now ? and what you want to print ? if you have 6 textviews then just do this

tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
Shubham Sharma
  • 2,763
  • 5
  • 31
  • 46
0

try this

String value="";
for(int i=1;i<6;i++)
{
    value = value + i + "\n";
}
textView.setText(value);
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95