Am facing some problem with edit text, actually what I need is 1st 10 letters typed should be in red colour and the letter's typed after this should be in grey colour. I did with the colour change, the issue is when I type the 11th letter, all the previously typed 10 letter's colour is also being changed. I want to keep the 1st 10 letter's colour constant, colour should be changed only after the 10th letter. Do help me in this please.
Asked
Active
Viewed 981 times
1
-
Try this piece of code [link](http://stackoverflow.com/a/34961310/2183890) – Ahsan Kamal Feb 10 '16 at 10:44
-
Use Spannable : Check [this answer](http://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview) – Alexandre BOURETZ Feb 10 '16 at 10:45
3 Answers
1
Try this code (I've tested it and works as expected):
final EditText input = (EditText) findViewById(R.id.my_edittext);
input.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) {
Spannable spannable = input.getText();
if (!TextUtils.isEmpty(s) && s.length() > 9){
spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this, R.color.my_color_red)),
0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
@Override
public void afterTextChanged(Editable s) {}
});

Anggrayudi H
- 14,977
- 11
- 54
- 87
0
Try using Spannable
Spannable spannable = myEditText.getText();
spannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), 0, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Rohit5k2
- 17,948
- 8
- 45
- 57
-
Am getting this error. java.lang.IndexOutOfBoundsException: setSpan (0 ... 11) ends beyond length 0 – aadhagupta Feb 10 '16 at 11:33
-
You need to set text to he edit text before calling this code. Check for its length before using this code. This should be called only if length of text is >= 10. Should use a text watcher listener. – Rohit5k2 Feb 10 '16 at 11:36
-
editText = (EditText) findViewById(R.id.edtext); editText.addTextChangedListener(passwordWatcher); }private final TextWatcher passwordWatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } public void afterTextChanged(Editable s) { if(s.toString().length()>10) { editText.setTextColor(getResources().getColor(android.R.color.holo_blue_dark)); } } – aadhagupta Feb 11 '16 at 03:40
-
0
Use Spannable
String for this as
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString redSpannable= new SpannableString("yourstring");
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, 11, 0);
builder.append(redSpannable);
SpannableString whiteSpannable= new SpannableString("next string");
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 11, 20, 0);
builder.append(whiteSpannable);
editText.setText(builder, BufferType.SPANNABLE);

Amarjit
- 4,327
- 2
- 34
- 51
-
i need the user to enter the text, i want a edit text not text view. text should be entered manually. – aadhagupta Feb 11 '16 at 03:42