What really happens when setText() is called on an EditText?











up vote
1
down vote

favorite












This is a part of the code:



editText.setText("Some Text", TextView.BufferType.EDITABLE);

Editable editable = (Editable) editText.getText();

// value of editable.toString() here is "Some Text"

editText.setText("Another Text", TextView.BufferType.EDITABLE);

// value of editable.toString() is still "Some Text"


Why the value of editable.toString() did not change? Thanks










share|improve this question


























    up vote
    1
    down vote

    favorite












    This is a part of the code:



    editText.setText("Some Text", TextView.BufferType.EDITABLE);

    Editable editable = (Editable) editText.getText();

    // value of editable.toString() here is "Some Text"

    editText.setText("Another Text", TextView.BufferType.EDITABLE);

    // value of editable.toString() is still "Some Text"


    Why the value of editable.toString() did not change? Thanks










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      This is a part of the code:



      editText.setText("Some Text", TextView.BufferType.EDITABLE);

      Editable editable = (Editable) editText.getText();

      // value of editable.toString() here is "Some Text"

      editText.setText("Another Text", TextView.BufferType.EDITABLE);

      // value of editable.toString() is still "Some Text"


      Why the value of editable.toString() did not change? Thanks










      share|improve this question













      This is a part of the code:



      editText.setText("Some Text", TextView.BufferType.EDITABLE);

      Editable editable = (Editable) editText.getText();

      // value of editable.toString() here is "Some Text"

      editText.setText("Another Text", TextView.BufferType.EDITABLE);

      // value of editable.toString() is still "Some Text"


      Why the value of editable.toString() did not change? Thanks







      android android-edittext textview android-editable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 at 15:33









      H. Riantsoa

      326




      326
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You assigned editText.getText() to a variable. That means its value won't change.



          When you call setText(), the original text is overwritten with the new CharSequence; the original instance of the Editable that getText() returns is no longer part of the TextView, so your editable variable is no longer attached to the TextView.



          Take a look at TextView's getEditableText() (this is what EditText calls from getText()):



          public Editable getEditableText() {
          return (mText instanceof Editable) ? (Editable) mText : null;
          }


          If mText is an Editable Object, then it'll return it. Otherwise, it'll return null.



          setText() eventually makes its way to setTextInternal():



          private void setTextInternal(@Nullable CharSequence text) {
          mText = text;
          mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
          mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
          }


          As you can see, it just overwrites the mText field, meaning your Editable instance is no longer the instance that the EditText has.



          TextView.java






          share|improve this answer





















          • I agree with most of this answer, but simply assigning a value to a variable doesn't mean that the value will never change. SpannableStringBuilder implements Editable, and so the results of its toString() method can change over time.
            – Ben P.
            Nov 13 at 15:46










          • @BenP. yes, but OP isn't using a SpannableStringBuilder. OP is using an EditText, which does overwrite the variable returned by getText(). That means that the editable variable OP has won't change when setText() is called.
            – TheWanderer
            Nov 13 at 15:50










          • My point is just that the blanket statement "You assigned X to a variable, therefore X's value won't change" is not true in general. It may be true in this case, but it would be a bad lesson to "learn" for java programming in general.
            – Ben P.
            Nov 13 at 15:53










          • It wasn't meant as a blanket statement.
            – TheWanderer
            Nov 13 at 15:54










          • ` Editable t = mEditableFactory.newEditable(text); text = t; ` also helped me a lot. Thanks
            – H. Riantsoa
            Nov 13 at 16:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284390%2fwhat-really-happens-when-settext-is-called-on-an-edittext%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You assigned editText.getText() to a variable. That means its value won't change.



          When you call setText(), the original text is overwritten with the new CharSequence; the original instance of the Editable that getText() returns is no longer part of the TextView, so your editable variable is no longer attached to the TextView.



          Take a look at TextView's getEditableText() (this is what EditText calls from getText()):



          public Editable getEditableText() {
          return (mText instanceof Editable) ? (Editable) mText : null;
          }


          If mText is an Editable Object, then it'll return it. Otherwise, it'll return null.



          setText() eventually makes its way to setTextInternal():



          private void setTextInternal(@Nullable CharSequence text) {
          mText = text;
          mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
          mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
          }


          As you can see, it just overwrites the mText field, meaning your Editable instance is no longer the instance that the EditText has.



          TextView.java






          share|improve this answer





















          • I agree with most of this answer, but simply assigning a value to a variable doesn't mean that the value will never change. SpannableStringBuilder implements Editable, and so the results of its toString() method can change over time.
            – Ben P.
            Nov 13 at 15:46










          • @BenP. yes, but OP isn't using a SpannableStringBuilder. OP is using an EditText, which does overwrite the variable returned by getText(). That means that the editable variable OP has won't change when setText() is called.
            – TheWanderer
            Nov 13 at 15:50










          • My point is just that the blanket statement "You assigned X to a variable, therefore X's value won't change" is not true in general. It may be true in this case, but it would be a bad lesson to "learn" for java programming in general.
            – Ben P.
            Nov 13 at 15:53










          • It wasn't meant as a blanket statement.
            – TheWanderer
            Nov 13 at 15:54










          • ` Editable t = mEditableFactory.newEditable(text); text = t; ` also helped me a lot. Thanks
            – H. Riantsoa
            Nov 13 at 16:54















          up vote
          2
          down vote



          accepted










          You assigned editText.getText() to a variable. That means its value won't change.



          When you call setText(), the original text is overwritten with the new CharSequence; the original instance of the Editable that getText() returns is no longer part of the TextView, so your editable variable is no longer attached to the TextView.



          Take a look at TextView's getEditableText() (this is what EditText calls from getText()):



          public Editable getEditableText() {
          return (mText instanceof Editable) ? (Editable) mText : null;
          }


          If mText is an Editable Object, then it'll return it. Otherwise, it'll return null.



          setText() eventually makes its way to setTextInternal():



          private void setTextInternal(@Nullable CharSequence text) {
          mText = text;
          mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
          mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
          }


          As you can see, it just overwrites the mText field, meaning your Editable instance is no longer the instance that the EditText has.



          TextView.java






          share|improve this answer





















          • I agree with most of this answer, but simply assigning a value to a variable doesn't mean that the value will never change. SpannableStringBuilder implements Editable, and so the results of its toString() method can change over time.
            – Ben P.
            Nov 13 at 15:46










          • @BenP. yes, but OP isn't using a SpannableStringBuilder. OP is using an EditText, which does overwrite the variable returned by getText(). That means that the editable variable OP has won't change when setText() is called.
            – TheWanderer
            Nov 13 at 15:50










          • My point is just that the blanket statement "You assigned X to a variable, therefore X's value won't change" is not true in general. It may be true in this case, but it would be a bad lesson to "learn" for java programming in general.
            – Ben P.
            Nov 13 at 15:53










          • It wasn't meant as a blanket statement.
            – TheWanderer
            Nov 13 at 15:54










          • ` Editable t = mEditableFactory.newEditable(text); text = t; ` also helped me a lot. Thanks
            – H. Riantsoa
            Nov 13 at 16:54













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You assigned editText.getText() to a variable. That means its value won't change.



          When you call setText(), the original text is overwritten with the new CharSequence; the original instance of the Editable that getText() returns is no longer part of the TextView, so your editable variable is no longer attached to the TextView.



          Take a look at TextView's getEditableText() (this is what EditText calls from getText()):



          public Editable getEditableText() {
          return (mText instanceof Editable) ? (Editable) mText : null;
          }


          If mText is an Editable Object, then it'll return it. Otherwise, it'll return null.



          setText() eventually makes its way to setTextInternal():



          private void setTextInternal(@Nullable CharSequence text) {
          mText = text;
          mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
          mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
          }


          As you can see, it just overwrites the mText field, meaning your Editable instance is no longer the instance that the EditText has.



          TextView.java






          share|improve this answer












          You assigned editText.getText() to a variable. That means its value won't change.



          When you call setText(), the original text is overwritten with the new CharSequence; the original instance of the Editable that getText() returns is no longer part of the TextView, so your editable variable is no longer attached to the TextView.



          Take a look at TextView's getEditableText() (this is what EditText calls from getText()):



          public Editable getEditableText() {
          return (mText instanceof Editable) ? (Editable) mText : null;
          }


          If mText is an Editable Object, then it'll return it. Otherwise, it'll return null.



          setText() eventually makes its way to setTextInternal():



          private void setTextInternal(@Nullable CharSequence text) {
          mText = text;
          mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
          mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
          }


          As you can see, it just overwrites the mText field, meaning your Editable instance is no longer the instance that the EditText has.



          TextView.java







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 15:40









          TheWanderer

          5,73611026




          5,73611026












          • I agree with most of this answer, but simply assigning a value to a variable doesn't mean that the value will never change. SpannableStringBuilder implements Editable, and so the results of its toString() method can change over time.
            – Ben P.
            Nov 13 at 15:46










          • @BenP. yes, but OP isn't using a SpannableStringBuilder. OP is using an EditText, which does overwrite the variable returned by getText(). That means that the editable variable OP has won't change when setText() is called.
            – TheWanderer
            Nov 13 at 15:50










          • My point is just that the blanket statement "You assigned X to a variable, therefore X's value won't change" is not true in general. It may be true in this case, but it would be a bad lesson to "learn" for java programming in general.
            – Ben P.
            Nov 13 at 15:53










          • It wasn't meant as a blanket statement.
            – TheWanderer
            Nov 13 at 15:54










          • ` Editable t = mEditableFactory.newEditable(text); text = t; ` also helped me a lot. Thanks
            – H. Riantsoa
            Nov 13 at 16:54


















          • I agree with most of this answer, but simply assigning a value to a variable doesn't mean that the value will never change. SpannableStringBuilder implements Editable, and so the results of its toString() method can change over time.
            – Ben P.
            Nov 13 at 15:46










          • @BenP. yes, but OP isn't using a SpannableStringBuilder. OP is using an EditText, which does overwrite the variable returned by getText(). That means that the editable variable OP has won't change when setText() is called.
            – TheWanderer
            Nov 13 at 15:50










          • My point is just that the blanket statement "You assigned X to a variable, therefore X's value won't change" is not true in general. It may be true in this case, but it would be a bad lesson to "learn" for java programming in general.
            – Ben P.
            Nov 13 at 15:53










          • It wasn't meant as a blanket statement.
            – TheWanderer
            Nov 13 at 15:54










          • ` Editable t = mEditableFactory.newEditable(text); text = t; ` also helped me a lot. Thanks
            – H. Riantsoa
            Nov 13 at 16:54
















          I agree with most of this answer, but simply assigning a value to a variable doesn't mean that the value will never change. SpannableStringBuilder implements Editable, and so the results of its toString() method can change over time.
          – Ben P.
          Nov 13 at 15:46




          I agree with most of this answer, but simply assigning a value to a variable doesn't mean that the value will never change. SpannableStringBuilder implements Editable, and so the results of its toString() method can change over time.
          – Ben P.
          Nov 13 at 15:46












          @BenP. yes, but OP isn't using a SpannableStringBuilder. OP is using an EditText, which does overwrite the variable returned by getText(). That means that the editable variable OP has won't change when setText() is called.
          – TheWanderer
          Nov 13 at 15:50




          @BenP. yes, but OP isn't using a SpannableStringBuilder. OP is using an EditText, which does overwrite the variable returned by getText(). That means that the editable variable OP has won't change when setText() is called.
          – TheWanderer
          Nov 13 at 15:50












          My point is just that the blanket statement "You assigned X to a variable, therefore X's value won't change" is not true in general. It may be true in this case, but it would be a bad lesson to "learn" for java programming in general.
          – Ben P.
          Nov 13 at 15:53




          My point is just that the blanket statement "You assigned X to a variable, therefore X's value won't change" is not true in general. It may be true in this case, but it would be a bad lesson to "learn" for java programming in general.
          – Ben P.
          Nov 13 at 15:53












          It wasn't meant as a blanket statement.
          – TheWanderer
          Nov 13 at 15:54




          It wasn't meant as a blanket statement.
          – TheWanderer
          Nov 13 at 15:54












          ` Editable t = mEditableFactory.newEditable(text); text = t; ` also helped me a lot. Thanks
          – H. Riantsoa
          Nov 13 at 16:54




          ` Editable t = mEditableFactory.newEditable(text); text = t; ` also helped me a lot. Thanks
          – H. Riantsoa
          Nov 13 at 16:54


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284390%2fwhat-really-happens-when-settext-is-called-on-an-edittext%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents