how to change text color without changing the the whole textview color
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to change the text color by simply clicking the button but when I click the button it changes the previously typed text color also. Means changes the whole text color. When I try to change color through Spannable then it changes the color of the selected part of the text but not for the text which I am going to type next. Is there a way where I can click the button and it does not change the whole text color but the color for which I type next. Thanks
here is what I know only
redtxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str.setSpan(new ForegroundColorSpan(Color.parseColor(txtColor)), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setTextColor(Color.parseColor("#d32f2f"));
}
});
java android eclipse
add a comment |
I am trying to change the text color by simply clicking the button but when I click the button it changes the previously typed text color also. Means changes the whole text color. When I try to change color through Spannable then it changes the color of the selected part of the text but not for the text which I am going to type next. Is there a way where I can click the button and it does not change the whole text color but the color for which I type next. Thanks
here is what I know only
redtxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str.setSpan(new ForegroundColorSpan(Color.parseColor(txtColor)), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setTextColor(Color.parseColor("#d32f2f"));
}
});
java android eclipse
Can u explain more about your requirement and also share your whole code like what isstr
and what istxtColor
– Nilesh Rathod
Nov 17 '18 at 4:26
Spannable str = text.getText(); int selStart = text.getSelectionStart(); int selEnd = text.getSelectionEnd(); ForegroundColorSpan styleSpans = text.getText().getSpans(selStart, selEnd, ForegroundColorSpan.class); and txtColor is the String which is the return value of button click for color.
– S Shah
Nov 17 '18 at 4:31
and what isstr
here can u please explain more about the same
– Nilesh Rathod
Nov 17 '18 at 4:34
str is Spannable name.
– S Shah
Nov 17 '18 at 4:35
@SShah , You need to implement Spannable String builder and onChangeText just append spanned string to Spannable String builder.
– Anil Ravsaheb Ghodake
Nov 17 '18 at 6:25
add a comment |
I am trying to change the text color by simply clicking the button but when I click the button it changes the previously typed text color also. Means changes the whole text color. When I try to change color through Spannable then it changes the color of the selected part of the text but not for the text which I am going to type next. Is there a way where I can click the button and it does not change the whole text color but the color for which I type next. Thanks
here is what I know only
redtxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str.setSpan(new ForegroundColorSpan(Color.parseColor(txtColor)), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setTextColor(Color.parseColor("#d32f2f"));
}
});
java android eclipse
I am trying to change the text color by simply clicking the button but when I click the button it changes the previously typed text color also. Means changes the whole text color. When I try to change color through Spannable then it changes the color of the selected part of the text but not for the text which I am going to type next. Is there a way where I can click the button and it does not change the whole text color but the color for which I type next. Thanks
here is what I know only
redtxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str.setSpan(new ForegroundColorSpan(Color.parseColor(txtColor)), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setTextColor(Color.parseColor("#d32f2f"));
}
});
java android eclipse
java android eclipse
edited Nov 17 '18 at 3:14
S Shah
asked Nov 17 '18 at 2:52
S ShahS Shah
156111
156111
Can u explain more about your requirement and also share your whole code like what isstr
and what istxtColor
– Nilesh Rathod
Nov 17 '18 at 4:26
Spannable str = text.getText(); int selStart = text.getSelectionStart(); int selEnd = text.getSelectionEnd(); ForegroundColorSpan styleSpans = text.getText().getSpans(selStart, selEnd, ForegroundColorSpan.class); and txtColor is the String which is the return value of button click for color.
– S Shah
Nov 17 '18 at 4:31
and what isstr
here can u please explain more about the same
– Nilesh Rathod
Nov 17 '18 at 4:34
str is Spannable name.
– S Shah
Nov 17 '18 at 4:35
@SShah , You need to implement Spannable String builder and onChangeText just append spanned string to Spannable String builder.
– Anil Ravsaheb Ghodake
Nov 17 '18 at 6:25
add a comment |
Can u explain more about your requirement and also share your whole code like what isstr
and what istxtColor
– Nilesh Rathod
Nov 17 '18 at 4:26
Spannable str = text.getText(); int selStart = text.getSelectionStart(); int selEnd = text.getSelectionEnd(); ForegroundColorSpan styleSpans = text.getText().getSpans(selStart, selEnd, ForegroundColorSpan.class); and txtColor is the String which is the return value of button click for color.
– S Shah
Nov 17 '18 at 4:31
and what isstr
here can u please explain more about the same
– Nilesh Rathod
Nov 17 '18 at 4:34
str is Spannable name.
– S Shah
Nov 17 '18 at 4:35
@SShah , You need to implement Spannable String builder and onChangeText just append spanned string to Spannable String builder.
– Anil Ravsaheb Ghodake
Nov 17 '18 at 6:25
Can u explain more about your requirement and also share your whole code like what is
str
and what is txtColor
– Nilesh Rathod
Nov 17 '18 at 4:26
Can u explain more about your requirement and also share your whole code like what is
str
and what is txtColor
– Nilesh Rathod
Nov 17 '18 at 4:26
Spannable str = text.getText(); int selStart = text.getSelectionStart(); int selEnd = text.getSelectionEnd(); ForegroundColorSpan styleSpans = text.getText().getSpans(selStart, selEnd, ForegroundColorSpan.class); and txtColor is the String which is the return value of button click for color.
– S Shah
Nov 17 '18 at 4:31
Spannable str = text.getText(); int selStart = text.getSelectionStart(); int selEnd = text.getSelectionEnd(); ForegroundColorSpan styleSpans = text.getText().getSpans(selStart, selEnd, ForegroundColorSpan.class); and txtColor is the String which is the return value of button click for color.
– S Shah
Nov 17 '18 at 4:31
and what is
str
here can u please explain more about the same– Nilesh Rathod
Nov 17 '18 at 4:34
and what is
str
here can u please explain more about the same– Nilesh Rathod
Nov 17 '18 at 4:34
str is Spannable name.
– S Shah
Nov 17 '18 at 4:35
str is Spannable name.
– S Shah
Nov 17 '18 at 4:35
@SShah , You need to implement Spannable String builder and onChangeText just append spanned string to Spannable String builder.
– Anil Ravsaheb Ghodake
Nov 17 '18 at 6:25
@SShah , You need to implement Spannable String builder and onChangeText just append spanned string to Spannable String builder.
– Anil Ravsaheb Ghodake
Nov 17 '18 at 6:25
add a comment |
3 Answers
3
active
oldest
votes
get your whole text into Spannable like
Spannable text = edittext.getText();
After that just do.
if (edittext.getText().length() != 0){
text.setSpan(new ForegroundColorSpan(YOUR_COLOR), selection_start, selection_end, 34);
}
It partially works but it does not work when I directly go to the next line then text color set to default. And also when I click near other span text it does not get its color from it.
– S Shah
Nov 17 '18 at 5:48
1
Then you need to code into TextWatcher() of Edittext. Apply above line every onTextChanged(). --->>>>> public void onTextChanged(CharSequence s1, final int start, final int before, final int count) { text.setSpan(new ForegroundColorSpan(color), start, start+1, 34); }
– Harshil kakadiya
Nov 17 '18 at 6:32
Now this works perfectly. Thanks. One more thing how can I get the color of adjacent text?? Let's suppose I typed previously in blue color and now I am typing in red color but when I want to add something in the blue text it takes that blue color from it and starts typing in the blue color in that area?
– S Shah
Nov 17 '18 at 17:46
add a comment |
you can use SpannableString
for example:
final SpannableString text = new SpannableString("Hello stackOverflow");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
I am using edit text which is Edittext text.
– S Shah
Nov 17 '18 at 4:34
I dont understand this part text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
– S Shah
Nov 17 '18 at 4:44
or see some example in this link :medium.com/@programmerr47/…
– saeedata
Nov 17 '18 at 5:09
add a comment |
You need to implement Spannable String builder(Global Object) and on onChangeText event just append spanned string to Spannable String builder.
SpannableStringBuilder objSpannableStringBuilder;
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
final SpannableString text = new SpannableString(s.toString());
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); objSpannableStringBuilder.append(text);
your_edit_text.setText(objSpannableStringBuilder);
}
@Override
public void afterTextChanged(Editable s) {
}
});
what is meant by "your_edit_text(Spannable String) comes ere" this in your code?
– S Shah
Nov 17 '18 at 17:18
@SShah It means your text changed string (Variable CahrSequense s). I just edited answer. Check it now
– Anil Ravsaheb Ghodake
Nov 19 '18 at 4:29
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%2f53347766%2fhow-to-change-text-color-without-changing-the-the-whole-textview-color%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
get your whole text into Spannable like
Spannable text = edittext.getText();
After that just do.
if (edittext.getText().length() != 0){
text.setSpan(new ForegroundColorSpan(YOUR_COLOR), selection_start, selection_end, 34);
}
It partially works but it does not work when I directly go to the next line then text color set to default. And also when I click near other span text it does not get its color from it.
– S Shah
Nov 17 '18 at 5:48
1
Then you need to code into TextWatcher() of Edittext. Apply above line every onTextChanged(). --->>>>> public void onTextChanged(CharSequence s1, final int start, final int before, final int count) { text.setSpan(new ForegroundColorSpan(color), start, start+1, 34); }
– Harshil kakadiya
Nov 17 '18 at 6:32
Now this works perfectly. Thanks. One more thing how can I get the color of adjacent text?? Let's suppose I typed previously in blue color and now I am typing in red color but when I want to add something in the blue text it takes that blue color from it and starts typing in the blue color in that area?
– S Shah
Nov 17 '18 at 17:46
add a comment |
get your whole text into Spannable like
Spannable text = edittext.getText();
After that just do.
if (edittext.getText().length() != 0){
text.setSpan(new ForegroundColorSpan(YOUR_COLOR), selection_start, selection_end, 34);
}
It partially works but it does not work when I directly go to the next line then text color set to default. And also when I click near other span text it does not get its color from it.
– S Shah
Nov 17 '18 at 5:48
1
Then you need to code into TextWatcher() of Edittext. Apply above line every onTextChanged(). --->>>>> public void onTextChanged(CharSequence s1, final int start, final int before, final int count) { text.setSpan(new ForegroundColorSpan(color), start, start+1, 34); }
– Harshil kakadiya
Nov 17 '18 at 6:32
Now this works perfectly. Thanks. One more thing how can I get the color of adjacent text?? Let's suppose I typed previously in blue color and now I am typing in red color but when I want to add something in the blue text it takes that blue color from it and starts typing in the blue color in that area?
– S Shah
Nov 17 '18 at 17:46
add a comment |
get your whole text into Spannable like
Spannable text = edittext.getText();
After that just do.
if (edittext.getText().length() != 0){
text.setSpan(new ForegroundColorSpan(YOUR_COLOR), selection_start, selection_end, 34);
}
get your whole text into Spannable like
Spannable text = edittext.getText();
After that just do.
if (edittext.getText().length() != 0){
text.setSpan(new ForegroundColorSpan(YOUR_COLOR), selection_start, selection_end, 34);
}
edited Nov 17 '18 at 5:34
answered Nov 17 '18 at 5:29
Harshil kakadiyaHarshil kakadiya
627
627
It partially works but it does not work when I directly go to the next line then text color set to default. And also when I click near other span text it does not get its color from it.
– S Shah
Nov 17 '18 at 5:48
1
Then you need to code into TextWatcher() of Edittext. Apply above line every onTextChanged(). --->>>>> public void onTextChanged(CharSequence s1, final int start, final int before, final int count) { text.setSpan(new ForegroundColorSpan(color), start, start+1, 34); }
– Harshil kakadiya
Nov 17 '18 at 6:32
Now this works perfectly. Thanks. One more thing how can I get the color of adjacent text?? Let's suppose I typed previously in blue color and now I am typing in red color but when I want to add something in the blue text it takes that blue color from it and starts typing in the blue color in that area?
– S Shah
Nov 17 '18 at 17:46
add a comment |
It partially works but it does not work when I directly go to the next line then text color set to default. And also when I click near other span text it does not get its color from it.
– S Shah
Nov 17 '18 at 5:48
1
Then you need to code into TextWatcher() of Edittext. Apply above line every onTextChanged(). --->>>>> public void onTextChanged(CharSequence s1, final int start, final int before, final int count) { text.setSpan(new ForegroundColorSpan(color), start, start+1, 34); }
– Harshil kakadiya
Nov 17 '18 at 6:32
Now this works perfectly. Thanks. One more thing how can I get the color of adjacent text?? Let's suppose I typed previously in blue color and now I am typing in red color but when I want to add something in the blue text it takes that blue color from it and starts typing in the blue color in that area?
– S Shah
Nov 17 '18 at 17:46
It partially works but it does not work when I directly go to the next line then text color set to default. And also when I click near other span text it does not get its color from it.
– S Shah
Nov 17 '18 at 5:48
It partially works but it does not work when I directly go to the next line then text color set to default. And also when I click near other span text it does not get its color from it.
– S Shah
Nov 17 '18 at 5:48
1
1
Then you need to code into TextWatcher() of Edittext. Apply above line every onTextChanged(). --->>>>> public void onTextChanged(CharSequence s1, final int start, final int before, final int count) { text.setSpan(new ForegroundColorSpan(color), start, start+1, 34); }
– Harshil kakadiya
Nov 17 '18 at 6:32
Then you need to code into TextWatcher() of Edittext. Apply above line every onTextChanged(). --->>>>> public void onTextChanged(CharSequence s1, final int start, final int before, final int count) { text.setSpan(new ForegroundColorSpan(color), start, start+1, 34); }
– Harshil kakadiya
Nov 17 '18 at 6:32
Now this works perfectly. Thanks. One more thing how can I get the color of adjacent text?? Let's suppose I typed previously in blue color and now I am typing in red color but when I want to add something in the blue text it takes that blue color from it and starts typing in the blue color in that area?
– S Shah
Nov 17 '18 at 17:46
Now this works perfectly. Thanks. One more thing how can I get the color of adjacent text?? Let's suppose I typed previously in blue color and now I am typing in red color but when I want to add something in the blue text it takes that blue color from it and starts typing in the blue color in that area?
– S Shah
Nov 17 '18 at 17:46
add a comment |
you can use SpannableString
for example:
final SpannableString text = new SpannableString("Hello stackOverflow");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
I am using edit text which is Edittext text.
– S Shah
Nov 17 '18 at 4:34
I dont understand this part text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
– S Shah
Nov 17 '18 at 4:44
or see some example in this link :medium.com/@programmerr47/…
– saeedata
Nov 17 '18 at 5:09
add a comment |
you can use SpannableString
for example:
final SpannableString text = new SpannableString("Hello stackOverflow");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
I am using edit text which is Edittext text.
– S Shah
Nov 17 '18 at 4:34
I dont understand this part text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
– S Shah
Nov 17 '18 at 4:44
or see some example in this link :medium.com/@programmerr47/…
– saeedata
Nov 17 '18 at 5:09
add a comment |
you can use SpannableString
for example:
final SpannableString text = new SpannableString("Hello stackOverflow");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
you can use SpannableString
for example:
final SpannableString text = new SpannableString("Hello stackOverflow");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
edited Nov 17 '18 at 10:39
answered Nov 17 '18 at 4:25
saeedatasaeedata
44137
44137
I am using edit text which is Edittext text.
– S Shah
Nov 17 '18 at 4:34
I dont understand this part text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
– S Shah
Nov 17 '18 at 4:44
or see some example in this link :medium.com/@programmerr47/…
– saeedata
Nov 17 '18 at 5:09
add a comment |
I am using edit text which is Edittext text.
– S Shah
Nov 17 '18 at 4:34
I dont understand this part text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
– S Shah
Nov 17 '18 at 4:44
or see some example in this link :medium.com/@programmerr47/…
– saeedata
Nov 17 '18 at 5:09
I am using edit text which is Edittext text.
– S Shah
Nov 17 '18 at 4:34
I am using edit text which is Edittext text.
– S Shah
Nov 17 '18 at 4:34
I dont understand this part text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
– S Shah
Nov 17 '18 at 4:44
I dont understand this part text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
– S Shah
Nov 17 '18 at 4:44
or see some example in this link :medium.com/@programmerr47/…
– saeedata
Nov 17 '18 at 5:09
or see some example in this link :medium.com/@programmerr47/…
– saeedata
Nov 17 '18 at 5:09
add a comment |
You need to implement Spannable String builder(Global Object) and on onChangeText event just append spanned string to Spannable String builder.
SpannableStringBuilder objSpannableStringBuilder;
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
final SpannableString text = new SpannableString(s.toString());
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); objSpannableStringBuilder.append(text);
your_edit_text.setText(objSpannableStringBuilder);
}
@Override
public void afterTextChanged(Editable s) {
}
});
what is meant by "your_edit_text(Spannable String) comes ere" this in your code?
– S Shah
Nov 17 '18 at 17:18
@SShah It means your text changed string (Variable CahrSequense s). I just edited answer. Check it now
– Anil Ravsaheb Ghodake
Nov 19 '18 at 4:29
add a comment |
You need to implement Spannable String builder(Global Object) and on onChangeText event just append spanned string to Spannable String builder.
SpannableStringBuilder objSpannableStringBuilder;
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
final SpannableString text = new SpannableString(s.toString());
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); objSpannableStringBuilder.append(text);
your_edit_text.setText(objSpannableStringBuilder);
}
@Override
public void afterTextChanged(Editable s) {
}
});
what is meant by "your_edit_text(Spannable String) comes ere" this in your code?
– S Shah
Nov 17 '18 at 17:18
@SShah It means your text changed string (Variable CahrSequense s). I just edited answer. Check it now
– Anil Ravsaheb Ghodake
Nov 19 '18 at 4:29
add a comment |
You need to implement Spannable String builder(Global Object) and on onChangeText event just append spanned string to Spannable String builder.
SpannableStringBuilder objSpannableStringBuilder;
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
final SpannableString text = new SpannableString(s.toString());
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); objSpannableStringBuilder.append(text);
your_edit_text.setText(objSpannableStringBuilder);
}
@Override
public void afterTextChanged(Editable s) {
}
});
You need to implement Spannable String builder(Global Object) and on onChangeText event just append spanned string to Spannable String builder.
SpannableStringBuilder objSpannableStringBuilder;
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
final SpannableString text = new SpannableString(s.toString());
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); objSpannableStringBuilder.append(text);
your_edit_text.setText(objSpannableStringBuilder);
}
@Override
public void afterTextChanged(Editable s) {
}
});
edited Nov 19 '18 at 4:33
answered Nov 17 '18 at 6:37
Anil Ravsaheb GhodakeAnil Ravsaheb Ghodake
94011937
94011937
what is meant by "your_edit_text(Spannable String) comes ere" this in your code?
– S Shah
Nov 17 '18 at 17:18
@SShah It means your text changed string (Variable CahrSequense s). I just edited answer. Check it now
– Anil Ravsaheb Ghodake
Nov 19 '18 at 4:29
add a comment |
what is meant by "your_edit_text(Spannable String) comes ere" this in your code?
– S Shah
Nov 17 '18 at 17:18
@SShah It means your text changed string (Variable CahrSequense s). I just edited answer. Check it now
– Anil Ravsaheb Ghodake
Nov 19 '18 at 4:29
what is meant by "your_edit_text(Spannable String) comes ere" this in your code?
– S Shah
Nov 17 '18 at 17:18
what is meant by "your_edit_text(Spannable String) comes ere" this in your code?
– S Shah
Nov 17 '18 at 17:18
@SShah It means your text changed string (Variable CahrSequense s). I just edited answer. Check it now
– Anil Ravsaheb Ghodake
Nov 19 '18 at 4:29
@SShah It means your text changed string (Variable CahrSequense s). I just edited answer. Check it now
– Anil Ravsaheb Ghodake
Nov 19 '18 at 4:29
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%2f53347766%2fhow-to-change-text-color-without-changing-the-the-whole-textview-color%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
Can u explain more about your requirement and also share your whole code like what is
str
and what istxtColor
– Nilesh Rathod
Nov 17 '18 at 4:26
Spannable str = text.getText(); int selStart = text.getSelectionStart(); int selEnd = text.getSelectionEnd(); ForegroundColorSpan styleSpans = text.getText().getSpans(selStart, selEnd, ForegroundColorSpan.class); and txtColor is the String which is the return value of button click for color.
– S Shah
Nov 17 '18 at 4:31
and what is
str
here can u please explain more about the same– Nilesh Rathod
Nov 17 '18 at 4:34
str is Spannable name.
– S Shah
Nov 17 '18 at 4:35
@SShah , You need to implement Spannable String builder and onChangeText just append spanned string to Spannable String builder.
– Anil Ravsaheb Ghodake
Nov 17 '18 at 6:25