1

How do we have hashtag prefixed word selected while typing where as normal for other words not prefixed with hashtag,

I am looking something like this Image Here

Here, while word prefixed with gets highlighted and when word followed by special character or space, highlighted content ends, and from then normal text continues

I tried with onTextChangedListener but it doesnt seem to work

 message.setText("#");
        message.addTextChangedListener(new TextWatcher() {

            int spaces =0;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String currentChar = String.valueOf(s.charAt(start));

                Log.v("Character",currentChar);

//                Log.v("character", String.valueOf(s)+ String.valueOf(start)+String.valueOf(before)+String.valueOf(count));
                Matcher m = Pattern.compile("[^A-Za-z0-9]").matcher(currentChar);
                if(!m.matches()){
                    Spannable spannable=new SpannableString(s);
                    spannable.setSpan(new StyleSpan(Typeface.BOLD), start, start, 0);
                    spannable.setSpan(new StyleSpan(Typeface.ITALIC), start, start, 0);
                    spannable.setSpan(new UnderlineSpan(), start, start, 0);
                    spannable.setSpan(new ForegroundColorSpan(Color.BLUE), start, start, 0);
                    message.setText(spannable);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
Jay kishan
  • 369
  • 2
  • 4
  • 9
  • Possible dup https://stackoverflow.com/questions/22291644/how-to-change-color-of-words-with-hashtags – Emmanuel Mtali Jun 20 '17 at 18:47
  • Related: https://stackoverflow.com/questions/12691679/android-autocomplete-textview-similar-to-the-facebook-app and https://stackoverflow.com/questions/8543449/how-to-use-the-textwatcher-class-in-android – CzarMatt Jun 20 '17 at 18:50
  • Thanks for the reply, I have alredy gone through the post, but my need is to change the color of the text while typing in the edittext but not for displaying purpose as displaying would just be fine. – Jay kishan Jun 20 '17 at 18:50
  • I have written a custom EditText class in kotlin to achieve this, I hope it helps someone; https://stackoverflow.com/a/57481803/2522797 – Tasneem Aug 13 '19 at 16:33

0 Answers0