Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid EditText : How to apply Foreground color span as you type?
    primarykey
    data
    text
    <p>I'm trying to apply a font color to text in an EditText as you type text. However, its just very inconsistent, meaning that sometimes if you type a space, the text preceding that space will go back to the default black color. Or if I put my cursor in the middle of a word and start typing the entire word changes color and not just the text I'm typing. The bold, italic and underline seem to work well though. How can I guarantee that only the text I'm typing will be affected with regards to font color? </p> <p>See "SIZE AND COLOR" comment below...</p> <pre><code> contentEdit.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { //add style as the user types if a toggle button is enabled ToggleButton boldButton = (ToggleButton) findViewById(R.id.bold); ToggleButton emButton = (ToggleButton) findViewById(R.id.italic); ToggleButton underlineButton = (ToggleButton) findViewById(R.id.underline); int position = Selection.getSelectionStart(contentEdit.getText()); try{ if (position &lt; 0){ position = 0; } if (position &gt; 0){ if (styleStart &gt; position || position &gt; (cursorLoc + 1)){ //user changed cursor location, reset if (position - cursorLoc &gt; 1){ //user pasted text styleStart = cursorLoc; } else{ styleStart = position - 1; } } if (boldButton.isChecked()){ StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class); for (int i = 0; i &lt; ss.length; i++) { if (ss[i].getStyle() == android.graphics.Typeface.BOLD){ s.removeSpan(ss[i]); } } s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (emButton.isChecked()){ StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class); for (int i = 0; i &lt; ss.length; i++) { if (ss[i].getStyle() == android.graphics.Typeface.ITALIC){ s.removeSpan(ss[i]); } } s.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (underlineButton.isChecked()){ UnderlineSpan[] ss = s.getSpans(styleStart, position, UnderlineSpan.class); for (int i = 0; i &lt; ss.length; i++) { s.removeSpan(ss[i]); } s.setSpan(new UnderlineSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } //SIZE AND COLOR////////////////////////////////////////////////////// s.setSpan(new ForegroundColorSpan(m_color), position, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE); s.setSpan(new AbsoluteSizeSpan(m_curSize, true), position, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE); } } catch(Exception e){ //Toast.makeText(m_ctx, m_ctx.gets, Toast.LENGTH_LONG).show(); showMessage(R.string.NOTE_WARNING_STYLE,m_utils.MSGTYPE_WARNING); } cursorLoc = Selection.getSelectionStart(contentEdit.getText()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload