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 ?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With