1

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.

this is my code, i want user to enter the text in live, after he enter a message of 10 letter,the 11th letter colour should be changed

aadhagupta
  • 47
  • 1
  • 8

3 Answers3

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
  • can u please help me out – aadhagupta Feb 11 '16 at 03:41
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