0

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.

4 Answers 4

1

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() );
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

Try:

text = new TextView(this);

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.