42

We all know we can setText(String) to an editText box but if you want to get an editable variable from a string variable how do you do this?

For Temp I made an invisible editText box to set to then get from which turns the string into editable.

re: for my own reference String again = editablevariable.getText().toString()

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 2
    Not sure what your trying to do. May be more information will help. – kosa Nov 12 '12 at 22:22
  • You want to make a copy of the string so you can modify it? If so, not necessary as you cannot modify String objects directly, it always result in a new String. If not, you need to add some more detail what you have and what you want (and what is not working) – Veger Nov 12 '12 at 22:25
  • see this post maybe help full http://stackoverflow.com/questions/2216201/editable-text-to-string – ρяσѕρєя K Nov 12 '12 at 22:25
  • Please post code to make your intentions clearer :) – Andy Nov 12 '12 at 22:29
  • I think I know what you are trying to ask: It can be described better in Kotlin though:

    say etUserName is an EditText,
    When you want to get a text from it:
    val strUserName = binding.etUserName.text.toString()
    then, when you have to set the text to it
    binding.etUserName.setText(signupFormParcel.strEmail)
    but you can't do the following directly:
    binding.etUserName.text = signupFormParcel.strEmail
    so you do:
    binding.etUserName.text = SpannableStringBuilder(signupFormParcel.strEmail)
    where strEmail is a string.
    – Abhinav Saxena Aug 26 '21 at 11:48

2 Answers2

81

As you probably found out, Editable is an interface so you cannot call new Editable().
However an Editable is nothing more than a String with Spannables, so use SpannableStringBuilder:

Editable editable = new SpannableStringBuilder("Pass a string here");

If you only want to change the Spannables, never the text itself, you can use a basic SpannableString.

Sam
  • 86,580
  • 20
  • 181
  • 179
11

Use Editable.Factory.getInstance().newEditable(str) From the android documentation:

Returns a new SpannedStringBuilder from the specified CharSequence. You can override this to provide a different kind of Spanned.

kyay10
  • 855
  • 8
  • 16
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/22242893) – Sree Feb 19 '19 at 06:22
  • 1
    @Sree It does answer it, what I am saying is that he should use this function with the string to convert it to editable. So simply str is the string that he wants to convert to an editable. – kyay10 Feb 19 '19 at 22:34
  • @Sree From the android documentation: https://developer.android.com/reference/android/text/Editable.Factory.html#newEditable(java.lang.CharSequence) – kyay10 Feb 19 '19 at 22:35
  • 1
    +1 : Cleaner than accepted answer, using a provided factory instead of relying on the type of underlying instance. – CyberEd Aug 28 '19 at 23:29