removeSpan(styleSpans[i]) remove the previous style from the styled text









up vote
2
down vote

favorite












I am trying to remove the bold text from the selected text but when I hit the button it unbolds all the text which I bolded previously. It seems that removeSpan.styleSpan[i] not taking care of what part of the text I selected instead it removes the bold text from the whole bolded span string.



i want the selected part to be unbold only as shown
enter image description here



but after hitting the button for remove span it unbolds the whole line like this
enter image description here



here is my code i am using for it under one button



 private void boldText()
int selectionStartb = texto.getSelectionStart();
int selectionEndb = texto.getSelectionEnd();

if (selectionStartb > selectionEndb)
int temp = selectionEndb;
selectionEndb = selectionStartb;
selectionStartb = temp;

if (selectionEndb > selectionStartb)
Spannable str = texto.getText();
boolean BL = false;
StyleSpan styleSpans;
styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);
// If the selected text-part already has BOLD style on it, then
// we need to disable it
for (int i = 0; i < styleSpans.length; i++)
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD)
str.removeSpan(styleSpans[i]);
BL = true;


// Else we set BOLD style on it
if (!BL)
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStartb, selectionEndb,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


texto.setSelection(selectionStartb, selectionEndb);












share|improve this question

























    up vote
    2
    down vote

    favorite












    I am trying to remove the bold text from the selected text but when I hit the button it unbolds all the text which I bolded previously. It seems that removeSpan.styleSpan[i] not taking care of what part of the text I selected instead it removes the bold text from the whole bolded span string.



    i want the selected part to be unbold only as shown
    enter image description here



    but after hitting the button for remove span it unbolds the whole line like this
    enter image description here



    here is my code i am using for it under one button



     private void boldText()
    int selectionStartb = texto.getSelectionStart();
    int selectionEndb = texto.getSelectionEnd();

    if (selectionStartb > selectionEndb)
    int temp = selectionEndb;
    selectionEndb = selectionStartb;
    selectionStartb = temp;

    if (selectionEndb > selectionStartb)
    Spannable str = texto.getText();
    boolean BL = false;
    StyleSpan styleSpans;
    styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);
    // If the selected text-part already has BOLD style on it, then
    // we need to disable it
    for (int i = 0; i < styleSpans.length; i++)
    if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD)
    str.removeSpan(styleSpans[i]);
    BL = true;


    // Else we set BOLD style on it
    if (!BL)
    str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStartb, selectionEndb,
    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


    texto.setSelection(selectionStartb, selectionEndb);












    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am trying to remove the bold text from the selected text but when I hit the button it unbolds all the text which I bolded previously. It seems that removeSpan.styleSpan[i] not taking care of what part of the text I selected instead it removes the bold text from the whole bolded span string.



      i want the selected part to be unbold only as shown
      enter image description here



      but after hitting the button for remove span it unbolds the whole line like this
      enter image description here



      here is my code i am using for it under one button



       private void boldText()
      int selectionStartb = texto.getSelectionStart();
      int selectionEndb = texto.getSelectionEnd();

      if (selectionStartb > selectionEndb)
      int temp = selectionEndb;
      selectionEndb = selectionStartb;
      selectionStartb = temp;

      if (selectionEndb > selectionStartb)
      Spannable str = texto.getText();
      boolean BL = false;
      StyleSpan styleSpans;
      styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);
      // If the selected text-part already has BOLD style on it, then
      // we need to disable it
      for (int i = 0; i < styleSpans.length; i++)
      if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD)
      str.removeSpan(styleSpans[i]);
      BL = true;


      // Else we set BOLD style on it
      if (!BL)
      str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStartb, selectionEndb,
      Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


      texto.setSelection(selectionStartb, selectionEndb);












      share|improve this question













      I am trying to remove the bold text from the selected text but when I hit the button it unbolds all the text which I bolded previously. It seems that removeSpan.styleSpan[i] not taking care of what part of the text I selected instead it removes the bold text from the whole bolded span string.



      i want the selected part to be unbold only as shown
      enter image description here



      but after hitting the button for remove span it unbolds the whole line like this
      enter image description here



      here is my code i am using for it under one button



       private void boldText()
      int selectionStartb = texto.getSelectionStart();
      int selectionEndb = texto.getSelectionEnd();

      if (selectionStartb > selectionEndb)
      int temp = selectionEndb;
      selectionEndb = selectionStartb;
      selectionStartb = temp;

      if (selectionEndb > selectionStartb)
      Spannable str = texto.getText();
      boolean BL = false;
      StyleSpan styleSpans;
      styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);
      // If the selected text-part already has BOLD style on it, then
      // we need to disable it
      for (int i = 0; i < styleSpans.length; i++)
      if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD)
      str.removeSpan(styleSpans[i]);
      BL = true;


      // Else we set BOLD style on it
      if (!BL)
      str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStartb, selectionEndb,
      Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


      texto.setSelection(selectionStartb, selectionEndb);









      java android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 1:41









      S Shah

      98110




      98110






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You're working with this text:




          Spannable str = texto.getText();



          And you're searching for spans within the selected range:




          styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);



          It's important to understand what's happening here. You are not getting all the "spanned text", or even all the StyleSpans, inside the selection; you are getting all the StyleSpans applied to the entire text, as long as at least part of them is applied to the selected range.



          In other words, even if you've selected only a portion of the spanned text, the StyleSpan object still represents the entire spanned text.



          You will have to modify your code to handle all the different possible scenarios:



          • The selected range has no spans on it -> add a span for the selected range

          • The selected range exactly matches a single span -> remove that span

          • The selected range is entirely spanned, but doesn't cover the whole span -> remove the existing span and re-add a span to the range that was previously spanned but not selected

          • The selected range is only partially spanned, and covers the whole span -> remove the existing span and add a span for the selected range

          • The selected range is only partially spanned, but doesn't cover the whole span -> remove the existing span and add a span for the selected range + the range that was previously spanned but not selected





          share|improve this answer




















          • Thank you so much for the detail. If you can tell me with the code i will better understand what you want to say. Thanks once again for your time. I will really appreciate that.
            – S Shah
            Nov 10 at 4:38










          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%2f53235308%2fremovespanstylespansi-remove-the-previous-style-from-the-styled-text%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
          0
          down vote













          You're working with this text:




          Spannable str = texto.getText();



          And you're searching for spans within the selected range:




          styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);



          It's important to understand what's happening here. You are not getting all the "spanned text", or even all the StyleSpans, inside the selection; you are getting all the StyleSpans applied to the entire text, as long as at least part of them is applied to the selected range.



          In other words, even if you've selected only a portion of the spanned text, the StyleSpan object still represents the entire spanned text.



          You will have to modify your code to handle all the different possible scenarios:



          • The selected range has no spans on it -> add a span for the selected range

          • The selected range exactly matches a single span -> remove that span

          • The selected range is entirely spanned, but doesn't cover the whole span -> remove the existing span and re-add a span to the range that was previously spanned but not selected

          • The selected range is only partially spanned, and covers the whole span -> remove the existing span and add a span for the selected range

          • The selected range is only partially spanned, but doesn't cover the whole span -> remove the existing span and add a span for the selected range + the range that was previously spanned but not selected





          share|improve this answer




















          • Thank you so much for the detail. If you can tell me with the code i will better understand what you want to say. Thanks once again for your time. I will really appreciate that.
            – S Shah
            Nov 10 at 4:38














          up vote
          0
          down vote













          You're working with this text:




          Spannable str = texto.getText();



          And you're searching for spans within the selected range:




          styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);



          It's important to understand what's happening here. You are not getting all the "spanned text", or even all the StyleSpans, inside the selection; you are getting all the StyleSpans applied to the entire text, as long as at least part of them is applied to the selected range.



          In other words, even if you've selected only a portion of the spanned text, the StyleSpan object still represents the entire spanned text.



          You will have to modify your code to handle all the different possible scenarios:



          • The selected range has no spans on it -> add a span for the selected range

          • The selected range exactly matches a single span -> remove that span

          • The selected range is entirely spanned, but doesn't cover the whole span -> remove the existing span and re-add a span to the range that was previously spanned but not selected

          • The selected range is only partially spanned, and covers the whole span -> remove the existing span and add a span for the selected range

          • The selected range is only partially spanned, but doesn't cover the whole span -> remove the existing span and add a span for the selected range + the range that was previously spanned but not selected





          share|improve this answer




















          • Thank you so much for the detail. If you can tell me with the code i will better understand what you want to say. Thanks once again for your time. I will really appreciate that.
            – S Shah
            Nov 10 at 4:38












          up vote
          0
          down vote










          up vote
          0
          down vote









          You're working with this text:




          Spannable str = texto.getText();



          And you're searching for spans within the selected range:




          styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);



          It's important to understand what's happening here. You are not getting all the "spanned text", or even all the StyleSpans, inside the selection; you are getting all the StyleSpans applied to the entire text, as long as at least part of them is applied to the selected range.



          In other words, even if you've selected only a portion of the spanned text, the StyleSpan object still represents the entire spanned text.



          You will have to modify your code to handle all the different possible scenarios:



          • The selected range has no spans on it -> add a span for the selected range

          • The selected range exactly matches a single span -> remove that span

          • The selected range is entirely spanned, but doesn't cover the whole span -> remove the existing span and re-add a span to the range that was previously spanned but not selected

          • The selected range is only partially spanned, and covers the whole span -> remove the existing span and add a span for the selected range

          • The selected range is only partially spanned, but doesn't cover the whole span -> remove the existing span and add a span for the selected range + the range that was previously spanned but not selected





          share|improve this answer












          You're working with this text:




          Spannable str = texto.getText();



          And you're searching for spans within the selected range:




          styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);



          It's important to understand what's happening here. You are not getting all the "spanned text", or even all the StyleSpans, inside the selection; you are getting all the StyleSpans applied to the entire text, as long as at least part of them is applied to the selected range.



          In other words, even if you've selected only a portion of the spanned text, the StyleSpan object still represents the entire spanned text.



          You will have to modify your code to handle all the different possible scenarios:



          • The selected range has no spans on it -> add a span for the selected range

          • The selected range exactly matches a single span -> remove that span

          • The selected range is entirely spanned, but doesn't cover the whole span -> remove the existing span and re-add a span to the range that was previously spanned but not selected

          • The selected range is only partially spanned, and covers the whole span -> remove the existing span and add a span for the selected range

          • The selected range is only partially spanned, but doesn't cover the whole span -> remove the existing span and add a span for the selected range + the range that was previously spanned but not selected






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 3:39









          Ben P.

          21.2k31845




          21.2k31845











          • Thank you so much for the detail. If you can tell me with the code i will better understand what you want to say. Thanks once again for your time. I will really appreciate that.
            – S Shah
            Nov 10 at 4:38
















          • Thank you so much for the detail. If you can tell me with the code i will better understand what you want to say. Thanks once again for your time. I will really appreciate that.
            – S Shah
            Nov 10 at 4:38















          Thank you so much for the detail. If you can tell me with the code i will better understand what you want to say. Thanks once again for your time. I will really appreciate that.
          – S Shah
          Nov 10 at 4:38




          Thank you so much for the detail. If you can tell me with the code i will better understand what you want to say. Thanks once again for your time. I will really appreciate that.
          – S Shah
          Nov 10 at 4:38

















          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%2f53235308%2fremovespanstylespansi-remove-the-previous-style-from-the-styled-text%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo