Printing method to TextView

Put your problem here if it does not fit any of the other categories.

Printing method to TextView

Postby harks44 » Sun Feb 26, 2012 2:43 pm

So System.out.println doesn't work in android, for obvious reasons. I'm trying to implement my java-game into android and need to find a substitute to make it work there. I'm new to this so I'm having trouble getting it to work.
What I want to do is printing my method to a TextView using setText.

instead of sys.out.println( //contents )

I want my textView p =

p.setText ( //contents)

doesn't work and reading the resource page at developer.android.com doesn't help me.. says setText only takes resid / array or charsequence.
harks44
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 26, 2012 1:48 pm

Top

Re: Printing method to TextView

Postby Phyll » Sun Feb 26, 2012 3:11 pm

Hi harks,

Generally I would say the Log method replaces (wraps) the sys.out.println. That is used for debugging mostly.

If you are trying to print text to the display you can do that in the ondraw if you are doing the drawing yourself in your game. If not, you can put a textview in your layout and then use settext as you have indicated like this:

Code: Select all
TextView mTitle;  //class scope declaration

mTitle = (TextView) findViewById(R.id.some_text_view);  //somewhere in code
mTitle.setText(some_String);


If you are trying to simulate a terminal there isn't any easy way unless you can find some code to do it already done. Such as in Android Terminal.

I don't understand "printing my method".

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Printing method to TextView

Postby harks44 » Sun Feb 26, 2012 4:27 pm

Thanks for the answer.
I have made a printBoard method in java looking like this:
public static void printBoard(Board board) {
// keeps track of tiles.
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
if (board.getTile(row, col).getValue() == 0)
System.out.print(" "); // keeps track of the empty tile and prints a blank instead of 0.
else
System.out.printf("%-3d", board.getTile(row, col)
.getValue()); // formats the board and keeps track of tiles and the values .
}

System.out.println(); // for formating
}
}
Now I want to convert it to Android.. tried something like this:

public void printBoard(){

print = (TextView) findViewById(R.id.tvPrint);

for(int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{

if (board.getTile(row, col).getValue() == 0)
print.setText(" ");
else
print.setText(board.getTile(row, col).getValue());
}
print.setText("\n");
}
}

This doesn't work at all crashes upon startup.
I quess it has something to do with my else statement.Tried use append and just written out 0-15 with print.append(String format( "%3s", row*4+col) and it works. It's my getTile method in my Board-class, but i want to refer to it just by method name instead of creating it all over again in printBoard method.
harks44
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 26, 2012 1:48 pm

Re: Printing method to TextView

Postby Phyll » Mon Feb 27, 2012 12:00 pm

Hi harks,

Well, without seeing the logcat and without seeing the code that leads up to this code, I don't see anything wrong with your code here and I can only guess that you haven't instantiated board at all before you pass it to this printBoard().

If you want a better answer, show us the logcat with the error and the code where you instantiate your Board class variable that you send to printBoard().

Thanks,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 23 guests