• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Print method to TextView

Harks44

Lurker
Hello Friends!

I am new to Android and I'm having problems. I have written a 15-game in java-code and now I'm about to implement it to an Android application.

I have written a method for printing out the state of the board to console in java with this:

public static void printBoard(Board board) {
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
if (board.getTile(row, col).getValue() == 0)
System.out.print(" ");
else
System.out.printf("%-3d",board.getTile(row,col)
.getValue());
}

System.out.println();
}

Now I have done the layout for the game and want to somehow write this method into a TextView. Problem is the System.out-print doens't work with android and the question is:

How can I write it into the TextView i've created? (deleting sys.out.print)
 
Wrong forum, but it seems that you can build up your string via concatenation and the use TextView's setText Method.
 
Okay sry, didn't see the development forum :)

TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);

this is quite helpful ... could I call my method in setText parameters? or is it impossible? Quite new to Java too so any type of code-example would be nice.

TextView tv = new TextView(this);
tv.setText(printBoard);
setContentView(tv);

printboard-method
if (board.getTile(row, col).getValue() == 0){
tv.setText(" ");
else
tv.setText( board.getTile(row, col).getValue());

Doesn't have my projekt available at the moment so couldn't test it out.

What I see in View.setText though is just setting from resid and from an array (and charsequence(?)). Can't get my head around it.


public final void setText (int resid, TextView.BufferType type)

Since: API Level 1
public final void setText (CharSequence text)

Since: API Level 1
Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.
Related XML Attributes

android:text
public void setText (CharSequence text, TextView.BufferType type)

Since: API Level 1
Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.
Related XML Attributes

android:text
android:bufferType
 
You could do something like this:

Code:
public static void printBoard(Board board, TextView tv) {
     for (int row = 0; row < 4; row++) {
         for (int col = 0; col < 4; col++) {
            if (board.getTile(row, col).getValue() == 0) 
                  tv.setText(" ");
            else
                  tv.setText(Double.parseDouble(board.getTile(row,col).getValue())); 
         }
    }
}
 
Back
Top Bottom