I have a TextView that I am trying to make display a 2d character-based grid. In my Java code I have created a 2d array, I take that array filled with strings and append each entry one next to another, and when the end of the array row is reached, i also append a newline character (.n). In this way I then take the big long singular string full of all the entries in the array (in order with the \n) and do setText(string). That way, the TextView is supposed to display a 2d grid on the display. It sort-of does this: the top row and the bottom row are both fine, but every row in between there are missing spaces between the end character in the row, and somewhere (approximately 13 characters) from the left side.
Here is the code snippets from making the array, filling it with characters, and making that array into a long string:
for(int row = 0; row < 22; row++){
for(int column = 0; column < 22; column++){
if(row == 0 || row == 21){
playField[row][column] = "#";
}else if(row > 0 && row < 21 ){
if(column == 0 || column == 21){
playField[row][column] = "#";
}else{
playField[row][column] = " ";
}
}
}
Now, the turning the array into a string:
temp = new String();
for(int i = 0; i < 22; i++){
for(int j = 0; j < 22; j++){
temp+=playField[i][j];
}
temp+="\n";
}return temp;
This string is what I use text.setText(temp) on, and it works, but it goes like this: # in column 1, about 10 blank spaces (as intended) then another # in column 13 or so, and then next row. On the top row, there are all 22 # signs, and on the bottom row there are all 22 # signs.
I have my textView set as such in the xml:
<TextView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
I think the problem is that you didn’t declared your TextView to use the monospace typeface, so all the spaces are there, but they are not as wide as the # symbol.
You can specify the typeface of the TextView like this: