Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text to a TextView datatype

Tags:

I'm a beginner android/java programmer and my background is primarily in C++ and C#. In C# if I have a string variable called myWord and it has a value of "Hello" I can append additional information by using the + operator.

I tried this method a few times in java and apparently I can't use this tatic because the TextView datatype is void. Android studio gives me the following error: Operator '+' cannot be applied to 'void', 'java.lang.String'

/*C # */ public string bob () { return "Bob!"; }  string myWord = "Hello "; myWord = myWord + "Bob!"; //myWord is now equal to "Hello Bob!"  OR   myWord = "Hello " + bob(); //myWord is now equal to "Hello Bob!"   */ JAVA */ TextView displayTextView = null; displayTextView.setText("Hello"); 

I'd like to find a way to append additional text to the original value of "Hello" so the value of displayTextView will be "Hello Bob!"

Any ideas?

Edit: I'm currently using a capital "S" and my app can successfully access and retrieve information from my class. The app would FC whenver I tried to append text to to the TextView data type.

Thank you for the feedback everyone.

like image 519
snapplex Avatar asked Apr 14 '15 19:04

snapplex


People also ask

Can a TextView be clickable?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

How do I add a newline to a TextView in android?

Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.

Which method is used to set the text in a TextView?

In android, we can set the text of TextView control either while declaring it in Layout file or by using setText() method in Activity file.

How do you load HTML content in TextView?

htmlToTextView. setText(HtmlCompat. fromHtml(htmlText, 0)); In the above code we are taking HTML data from fromHtml() and appending to textview using setText() .


1 Answers

You can call append() on a TextView object.

In your case it would be: displayTextView.append("Bob!");

like image 86
Longi Avatar answered Sep 20 '22 18:09

Longi