Add extra line in SpannableStringBuilder getSpans()
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
add a comment |
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
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 addn
after eachURLSpan
– pskink
Nov 14 '18 at 7:36
add a comment |
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
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
android url spannablestring spannablestringbuilder
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 addn
after eachURLSpan
– pskink
Nov 14 '18 at 7:36
add a comment |
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 addn
after eachURLSpan
– 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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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())
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 14 '18 at 7:34
Pooja GaonkarPooja Gaonkar
1,1641223
1,1641223
add a comment |
add a comment |
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())
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
add a comment |
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())
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
add a comment |
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())
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())
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 eachURLSpan
– pskink
Nov 14 '18 at 7:36