Converting TextView to String (kind of) Android

72,693

Solution 1

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

String sharedFact = theFact.getText().toString();

Solution 2

TextView theFact = (TextView) findViewById(R.id.theFact);
String shareFact = theFact.getText().toString();
Share:
72,693
Keenan Thompson
Author by

Keenan Thompson

Updated on July 09, 2022

Comments

  • Keenan Thompson
    Keenan Thompson almost 2 years

    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!

  • Keenan Thompson
    Keenan Thompson over 13 years
    Thanks... again! You answered my last question too!
  • Adan Vivero
    Adan Vivero over 2 years
    How is this done using Kotlin?
  • Uncle Roger
    Uncle Roger about 2 years
    It doesn't work. Please check my problem. Here
  • Uncle Roger
    Uncle Roger about 2 years
    It doesn't work. Please check my problem. Here