Change text color as custom hash tags

I am working on an application in which a user can post content in a feed. In my text editor (used for layout) the text color is gray. However, when the user enters a hash tag, for example. #help I need to color this text black by type, so when the user types “#”, the text should be painted black until a new word begins, then the color of the text should return to gray.

I am trying to use a text observer and a stretched string for color.

Here is what I did with textwatcher onTextChanged

  @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //Remove Text watcher to prevent infinite look mTextField.removeTextChangedListener(contentFieldWatcher); String startChar = null; startChar = Character.toString(s.charAt(start)); Li(getClass().getSimpleName(), "CHARACTER OF NEW WORD: " + startChar); if (startChar.equals("#")) { tagCheck(s.toString().substring(start), start, start + count); } } 

tag method

 private void tagCheck(String s, int start, int end) { mSpannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.black_colour)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } 

mSpannable is spammy.

The problem with this method is that '#' is displayed as startChar, however, when the user enters the next character, character or letter, it shows as startChar. Where, as if the user typed santa - s', startChar remains. Therefore, the problem I am facing is how to dynamically color the text when the user enters a hashtag.

So simple letters work correctly, however, when you use a symbol, it is not. I hope the question is clear. I looked at it for several days and it all got foggy :)

+5
source share
2 answers

I tried and found soloution

You can use the following code

 Spannable mspanable; int hashTagIsComing = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText edt = (EditText) findViewById(R.id.editText1); mspanable = edt.getText(); edt.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String startChar = null; try{ startChar = Character.toString(s.charAt(start)); Log.i(getClass().getSimpleName(), "CHARACTER OF NEW WORD: " + startChar); } catch(Exception ex){ startChar = " "; } if (startChar.equals("#")) { tagCheck(s.toString().substring(start), start, start + count); hashTagIsComing++; } if(startChar.equals(" ")){ hashTagIsComing = 0; } if(hashTagIsComing != 0) { tagCheck(s.toString().substring(start), start, start + count); hashTagIsComing++; } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); } private void tagCheck(String s, int start, int end) { mspanable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } 
+4
source

I managed to achieve the described behavior by simply setting the colored text to a separate TextView. See screenshot and code below. Hope this helps

In afterTextChanged from the listener:

 @Override public void afterTextChanged(Editable s) { editText.removeTextChangedListener(textWatcher); colorText(s.toString()); } 

The search method "#" characters and colored text to the first place:

 private void colorText(String s) { if (!TextUtils.isEmpty(s)) { Spannable spannable = new SpannableString(s); int position = 0; position = s.indexOf("#", position); while (position != -1) { colorSpannable(spannable, position, s.indexOf(" ", position + 1) != -1 ? s.indexOf(" ", position + 1) : s.length()); position = s.indexOf("#", position + 1); } textView.setText(spannable); } editText.addTextChangedListener(textWatcher); } 

colorSpannable simply adds color from the beginning of the index to the end of the index

 private void colorSpannable(Spannable s, int start, int end){ s.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } 

enter image description here

+1
source

Source: https://habr.com/ru/post/1242526/


All Articles