Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting TextView to String (kind of) Android

I have an object on my main.xml layout file called thefact that is TextView. I also have a string called sharefact. I want to save what text is in the TextView into the sharefact string. I can't do:

sharefact = thefact

Or anything similar to that because it would want me to convert sharefact into a textview.

Thanks!

like image 811
Keenan Thompson Avatar asked Oct 13 '10 23:10

Keenan Thompson


People also ask

What is the return type of GetText in android?

GetText returns the text from the single-line text field. It returns only the first line of a multi-line text field.

Which method is used to change the text size of the TextView in android?

To use preset sizes to set up the autosizing of TextView programmatically, call the setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) method. Provide an array of sizes and any TypedValue dimension unit for the size.

What is the use of setText in android?

SetText(String, TextView+BufferType)Sets the text to be displayed using a string resource identifier.

What is the difference between edit text and text TextView android?

EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.


2 Answers

TextView theFact = (TextView) findViewById(R.id.theFact);
String shareFact = theFact.getText().toString();
like image 200
Tyler Treat Avatar answered Sep 19 '22 19:09

Tyler Treat


Getting text from a TextView returns CharSequence. To convert CharSequence to String, toString() function is used.

String sharedFact = theFact.getText().toString();
like image 37
kiki Avatar answered Sep 19 '22 19:09

kiki