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
but after hitting the button for remove span it unbolds the whole line like this
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
add a comment |
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
but after hitting the button for remove span it unbolds the whole line like this
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
add a comment |
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
but after hitting the button for remove span it unbolds the whole line like this
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
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
but after hitting the button for remove span it unbolds the whole line like this
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
java android
asked Nov 10 at 1:41
S Shah
98110
98110
add a comment |
add a comment |
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 StyleSpan
s, inside the selection; you are getting all the StyleSpan
s 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
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
add a comment |
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 StyleSpan
s, inside the selection; you are getting all the StyleSpan
s 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
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
add a comment |
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 StyleSpan
s, inside the selection; you are getting all the StyleSpan
s 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
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
add a comment |
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 StyleSpan
s, inside the selection; you are getting all the StyleSpan
s 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
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 StyleSpan
s, inside the selection; you are getting all the StyleSpan
s 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
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
add a comment |
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
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.
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.
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%2f53235308%2fremovespanstylespansi-remove-the-previous-style-from-the-styled-text%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