Jagreet
Jagreet

Reputation: 113

TextView.getText().toString() vs TextView.toString()

Both methods do the same thing but on most forums I see that the first one is used but hardly the second. Is the second one the wrong way to obtain a String from a TextView or does the first method hold some advantages over the second as it is used more frequently ?

Upvotes: 2

Views: 2184

Answers (2)

Sagar
Sagar

Reputation: 24917

textView.getText() returns CharSequence. To convert CharSequence to String, toString() method is used. Hence we do textView.getText().toString(). This method is widely used to get the text in string format from the TextView.

TextView.toString() on the other hand returns the string representation of the TextView object which is defined in toString() method for TextView class.

To elaborate with example:

Lets say we have a TextView with "Hello World" as text which appears on screen.

textViewobj.getText().toString() will return:

Hello World

Where as textViewObj.toString() will return value similar to following:

android.support.v7.widget.AppCompatTextView{58a835e V.ED..... ......ID 0,0-0,0 #7f0800a3 app:id/textView}

This is in memory string representation of TextView.

Upvotes: 7

Santanu Sur
Santanu Sur

Reputation: 11487

TextView.getText().toString() --

Gives the string value which we can SEE visually inside the textView widget.

TextView.toString() --

Gives the string reference of the TextView object in the memory. This value can be something like this :-

   @2848&8berx   or,
   @89jccxyzsjjx

Upvotes: 1

Related Questions