cApple
cApple

Reputation: 311

Android Displaying more than 1 variable textView

I have a textView and I want to display two different variables at the same time, each on one line. Here is what I have so far

TextView.setText("You answered :" + " " + correct + "correct" + '\n');
TextView.setText("You answered: " + " " + wrong + "incorrect");

this only displays the last line and not both lines of code in the textView. Can anyone tell me how i can go about displaying both of these in the same textView at the same time on 2 different lines? Much appreciated.

Upvotes: 0

Views: 1245

Answers (5)

Tuan Vu
Tuan Vu

Reputation: 6447

Using this snip code:

TextView.setText("You answered :" + " " + correct + "correct\n" +"You answered: " + " " + wrong + "incorrect");

Upvotes: 2

Ken Y-N
Ken Y-N

Reputation: 15009

Isn't the generic answer:

TextView.setText("You answered :" + " " + correct + "correct" + '\n');
// Lots of other intervening code
TextView.append("You answered: " + " " + wrong + "incorrect");

In addition, there is a lot of unnecessary string operations:

TextView.setText("You answered : " + correct + "correct\n");
// Lots of other intervening code
TextView.append("You answered:  " + wrong + " incorrect");

Upvotes: -1

ThomasP1975
ThomasP1975

Reputation: 11

Try create the String first then set the text in the TextView.

Upvotes: 0

David Manpearl
David Manpearl

Reputation: 12656

Try this:

TextView.setText("You answered: " + correct + "correct\nYou answered: " + wrong + "incorrect");

Note: By convention, you should avoid naming variables with capital letters, especially if the name is already a class name. Consider using textView instead of TextView.

Upvotes: 1

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

String text = "You answered :" + " " + correct + "correct\n" +"You answered: " + " " + wrong + "incorrect";
TextView.setText(text);

Upvotes: 2

Related Questions