Add extra line in SpannableStringBuilder getSpans()










1















I am combining different strings to show in a TextView. I am doing this using a SpannableStringBuilder, as I need to apply a different color to a few substrings in the string. Now in the combined strings, there are urls which I show in different colors along with some other text. I am doing something like below.



Spannable spannedText = (Spannable) Util.getFormattedText(plainText);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(spannedText);
URLSpan urls = strBuilder.getSpans(0, spannedText.length(), URLSpan.class);
for (URLSpan span : urls)

//apply clicks



Output is : Link1 Link2 some links here.



I want to show each url on a different line with the rest of the text after the urls.



eg.



Link1
Link2
some links here


But I am not able to find a solution for this. Any help is appreciated.










share|improve this question
























  • n does not work?

    – pskink
    Nov 14 '18 at 7:34











  • @pskink No. The links I want to show are dynamic, so I don't know how many links I am going to receive.

    – user3034944
    Nov 14 '18 at 7:35











  • so what if they are dynamic? i dont see any problem here - just add n after each URLSpan

    – pskink
    Nov 14 '18 at 7:36















1















I am combining different strings to show in a TextView. I am doing this using a SpannableStringBuilder, as I need to apply a different color to a few substrings in the string. Now in the combined strings, there are urls which I show in different colors along with some other text. I am doing something like below.



Spannable spannedText = (Spannable) Util.getFormattedText(plainText);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(spannedText);
URLSpan urls = strBuilder.getSpans(0, spannedText.length(), URLSpan.class);
for (URLSpan span : urls)

//apply clicks



Output is : Link1 Link2 some links here.



I want to show each url on a different line with the rest of the text after the urls.



eg.



Link1
Link2
some links here


But I am not able to find a solution for this. Any help is appreciated.










share|improve this question
























  • n does not work?

    – pskink
    Nov 14 '18 at 7:34











  • @pskink No. The links I want to show are dynamic, so I don't know how many links I am going to receive.

    – user3034944
    Nov 14 '18 at 7:35











  • so what if they are dynamic? i dont see any problem here - just add n after each URLSpan

    – pskink
    Nov 14 '18 at 7:36













1












1








1








I am combining different strings to show in a TextView. I am doing this using a SpannableStringBuilder, as I need to apply a different color to a few substrings in the string. Now in the combined strings, there are urls which I show in different colors along with some other text. I am doing something like below.



Spannable spannedText = (Spannable) Util.getFormattedText(plainText);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(spannedText);
URLSpan urls = strBuilder.getSpans(0, spannedText.length(), URLSpan.class);
for (URLSpan span : urls)

//apply clicks



Output is : Link1 Link2 some links here.



I want to show each url on a different line with the rest of the text after the urls.



eg.



Link1
Link2
some links here


But I am not able to find a solution for this. Any help is appreciated.










share|improve this question
















I am combining different strings to show in a TextView. I am doing this using a SpannableStringBuilder, as I need to apply a different color to a few substrings in the string. Now in the combined strings, there are urls which I show in different colors along with some other text. I am doing something like below.



Spannable spannedText = (Spannable) Util.getFormattedText(plainText);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(spannedText);
URLSpan urls = strBuilder.getSpans(0, spannedText.length(), URLSpan.class);
for (URLSpan span : urls)

//apply clicks



Output is : Link1 Link2 some links here.



I want to show each url on a different line with the rest of the text after the urls.



eg.



Link1
Link2
some links here


But I am not able to find a solution for this. Any help is appreciated.







android url spannablestring spannablestringbuilder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 8:27









Nirav Bhavsar

7111513




7111513










asked Nov 14 '18 at 7:17









user3034944user3034944

2212521




2212521












  • n does not work?

    – pskink
    Nov 14 '18 at 7:34











  • @pskink No. The links I want to show are dynamic, so I don't know how many links I am going to receive.

    – user3034944
    Nov 14 '18 at 7:35











  • so what if they are dynamic? i dont see any problem here - just add n after each URLSpan

    – pskink
    Nov 14 '18 at 7:36

















  • n does not work?

    – pskink
    Nov 14 '18 at 7:34











  • @pskink No. The links I want to show are dynamic, so I don't know how many links I am going to receive.

    – user3034944
    Nov 14 '18 at 7:35











  • so what if they are dynamic? i dont see any problem here - just add n after each URLSpan

    – pskink
    Nov 14 '18 at 7:36
















n does not work?

– pskink
Nov 14 '18 at 7:34





n does not work?

– pskink
Nov 14 '18 at 7:34













@pskink No. The links I want to show are dynamic, so I don't know how many links I am going to receive.

– user3034944
Nov 14 '18 at 7:35





@pskink No. The links I want to show are dynamic, so I don't know how many links I am going to receive.

– user3034944
Nov 14 '18 at 7:35













so what if they are dynamic? i dont see any problem here - just add n after each URLSpan

– pskink
Nov 14 '18 at 7:36





so what if they are dynamic? i dont see any problem here - just add n after each URLSpan

– pskink
Nov 14 '18 at 7:36












2 Answers
2






active

oldest

votes


















3














You could do like what is mentioned in this answer.



Just use your UrlSpan instead of ImageSpan like below :-



for (URLSpan span : urls)

strBuilder = strBuilder.insert(strBuilder.getSpanEnd(span), System.getProperty("line.separator"));
//apply clicks






share|improve this answer






























    0














    Use SpanableString



     SpannableString spanUrl1 = new SpannableString("www.google.com");
    spanUrl1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, spanUrl1.length(),
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    Spannable spanUrl2 = new SpannableString("www.stackoverflow.com");
    spanUrl2.setSpan(new ForegroundColorSpan(Color.RED), 0, spanUrl2.length(),
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    TV.setText(TextUtils.concat(spanUrl1, "n",spanUrl2));
    TV.setMovementMethod(LinkMovementMethod.getInstance())





    share|improve this answer

























    • Thanks for the reply. Where does this code put the links on separate line?

      – user3034944
      Nov 14 '18 at 7:27











    • I am already applying colors to the different sections. My issue is that I need them to be on separate lines

      – user3034944
      Nov 14 '18 at 7:28











    • @user3034944 check my answer i had updated my answer.

      – Milan Pansuriya
      Nov 14 '18 at 7:30










    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',
    autoActivateHeartbeat: false,
    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%2f53294916%2fadd-extra-line-in-spannablestringbuilder-getspans%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You could do like what is mentioned in this answer.



    Just use your UrlSpan instead of ImageSpan like below :-



    for (URLSpan span : urls)

    strBuilder = strBuilder.insert(strBuilder.getSpanEnd(span), System.getProperty("line.separator"));
    //apply clicks






    share|improve this answer



























      3














      You could do like what is mentioned in this answer.



      Just use your UrlSpan instead of ImageSpan like below :-



      for (URLSpan span : urls)

      strBuilder = strBuilder.insert(strBuilder.getSpanEnd(span), System.getProperty("line.separator"));
      //apply clicks






      share|improve this answer

























        3












        3








        3







        You could do like what is mentioned in this answer.



        Just use your UrlSpan instead of ImageSpan like below :-



        for (URLSpan span : urls)

        strBuilder = strBuilder.insert(strBuilder.getSpanEnd(span), System.getProperty("line.separator"));
        //apply clicks






        share|improve this answer













        You could do like what is mentioned in this answer.



        Just use your UrlSpan instead of ImageSpan like below :-



        for (URLSpan span : urls)

        strBuilder = strBuilder.insert(strBuilder.getSpanEnd(span), System.getProperty("line.separator"));
        //apply clicks







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 7:34









        Pooja GaonkarPooja Gaonkar

        1,1641223




        1,1641223























            0














            Use SpanableString



             SpannableString spanUrl1 = new SpannableString("www.google.com");
            spanUrl1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, spanUrl1.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            Spannable spanUrl2 = new SpannableString("www.stackoverflow.com");
            spanUrl2.setSpan(new ForegroundColorSpan(Color.RED), 0, spanUrl2.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            TV.setText(TextUtils.concat(spanUrl1, "n",spanUrl2));
            TV.setMovementMethod(LinkMovementMethod.getInstance())





            share|improve this answer

























            • Thanks for the reply. Where does this code put the links on separate line?

              – user3034944
              Nov 14 '18 at 7:27











            • I am already applying colors to the different sections. My issue is that I need them to be on separate lines

              – user3034944
              Nov 14 '18 at 7:28











            • @user3034944 check my answer i had updated my answer.

              – Milan Pansuriya
              Nov 14 '18 at 7:30















            0














            Use SpanableString



             SpannableString spanUrl1 = new SpannableString("www.google.com");
            spanUrl1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, spanUrl1.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            Spannable spanUrl2 = new SpannableString("www.stackoverflow.com");
            spanUrl2.setSpan(new ForegroundColorSpan(Color.RED), 0, spanUrl2.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            TV.setText(TextUtils.concat(spanUrl1, "n",spanUrl2));
            TV.setMovementMethod(LinkMovementMethod.getInstance())





            share|improve this answer

























            • Thanks for the reply. Where does this code put the links on separate line?

              – user3034944
              Nov 14 '18 at 7:27











            • I am already applying colors to the different sections. My issue is that I need them to be on separate lines

              – user3034944
              Nov 14 '18 at 7:28











            • @user3034944 check my answer i had updated my answer.

              – Milan Pansuriya
              Nov 14 '18 at 7:30













            0












            0








            0







            Use SpanableString



             SpannableString spanUrl1 = new SpannableString("www.google.com");
            spanUrl1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, spanUrl1.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            Spannable spanUrl2 = new SpannableString("www.stackoverflow.com");
            spanUrl2.setSpan(new ForegroundColorSpan(Color.RED), 0, spanUrl2.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            TV.setText(TextUtils.concat(spanUrl1, "n",spanUrl2));
            TV.setMovementMethod(LinkMovementMethod.getInstance())





            share|improve this answer















            Use SpanableString



             SpannableString spanUrl1 = new SpannableString("www.google.com");
            spanUrl1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, spanUrl1.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            Spannable spanUrl2 = new SpannableString("www.stackoverflow.com");
            spanUrl2.setSpan(new ForegroundColorSpan(Color.RED), 0, spanUrl2.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            TV.setText(TextUtils.concat(spanUrl1, "n",spanUrl2));
            TV.setMovementMethod(LinkMovementMethod.getInstance())






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 7:29

























            answered Nov 14 '18 at 7:25









            Milan PansuriyaMilan Pansuriya

            1,6211719




            1,6211719












            • Thanks for the reply. Where does this code put the links on separate line?

              – user3034944
              Nov 14 '18 at 7:27











            • I am already applying colors to the different sections. My issue is that I need them to be on separate lines

              – user3034944
              Nov 14 '18 at 7:28











            • @user3034944 check my answer i had updated my answer.

              – Milan Pansuriya
              Nov 14 '18 at 7:30

















            • Thanks for the reply. Where does this code put the links on separate line?

              – user3034944
              Nov 14 '18 at 7:27











            • I am already applying colors to the different sections. My issue is that I need them to be on separate lines

              – user3034944
              Nov 14 '18 at 7:28











            • @user3034944 check my answer i had updated my answer.

              – Milan Pansuriya
              Nov 14 '18 at 7:30
















            Thanks for the reply. Where does this code put the links on separate line?

            – user3034944
            Nov 14 '18 at 7:27





            Thanks for the reply. Where does this code put the links on separate line?

            – user3034944
            Nov 14 '18 at 7:27













            I am already applying colors to the different sections. My issue is that I need them to be on separate lines

            – user3034944
            Nov 14 '18 at 7:28





            I am already applying colors to the different sections. My issue is that I need them to be on separate lines

            – user3034944
            Nov 14 '18 at 7:28













            @user3034944 check my answer i had updated my answer.

            – Milan Pansuriya
            Nov 14 '18 at 7:30





            @user3034944 check my answer i had updated my answer.

            – Milan Pansuriya
            Nov 14 '18 at 7:30

















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294916%2fadd-extra-line-in-spannablestringbuilder-getspans%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto