user6274128
user6274128

Reputation:

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

Upvotes: 0

Views: 281

Answers (3)

KDeogharkar
KDeogharkar

Reputation: 10959

try this

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

Upvotes: 0

Mohammad Rahchamani
Mohammad Rahchamani

Reputation: 5220

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");
}

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 2793

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+"");

Upvotes: 0

Related Questions