sachit
sachit

Reputation: 1138

how to print array elements in nextline on textView in android

hello everyone i have a 2D array. I am printing the elements of this array on a text View. But my all elements are coming in one row.I want line break between elements. my code is given below and my 2D array is:

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}}

for(i=0;i<8;i++)
for(j=0;j<2;j++)
{
text = new TextView(this);
text.setText(""+table[j][i]);
}

and i am getting output like this:

{1,8,2,7,3,6,4,5,5,4,6,3,7,2,8,1}

but i want output like this:

1,8
2,7
3,6
4,5
5,4
6,3
7,2
8,1 

any help would be appreciated.

Upvotes: 0

Views: 1940

Answers (4)

Lucifer
Lucifer

Reputation: 29642

You need to add Escape sequence \n.

Try this out,

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}}

text = new TextView(this);
StringBuffer sb = new StringBuffer();

for(i=0;i<8;i++)
for(j=0;j<2;j++)
{
    sb.append ( "" + table[j][i] + "\n" );
}

text.setText( sb.toString() );

Upvotes: 1

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

This will Add New Line after every two Numbers.

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}}
String str="";
for(i=0;i<8;i++)
{
  for(j=0;j<2;j++)
  {  
    str=str+table[j][i];
  }
  str=str+"\n";
}
text = new TextView(this);
text.setText(Html.fromHtml(str));

Upvotes: 0

sandrstar
sandrstar

Reputation: 12643

I would suggest not to create TextView in the loop. Just use same view:

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}}
text = new TextView(this);

for(i=0;i<8;i++) {
    for(j=0;j<2;j++) {
        text.append(""+table[j][i] + "\n");
    }
}

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

Try:

text = new TextView(this);

for loop {
        text.append(""+table[j][i] + "\n");
}

Upvotes: 0

Related Questions