1

I want to change the color of one word when I touch it.

For example: Hello, my name is Robert.

(by default, all black)

Now if the user touches the word "Robert", I want the color of "Robert" to change to RED.

How do I do that? (I'm new to Android)

textview.setTextColor() changes the whole thing, I only want one word.

Stuffy
  • 13
  • 3
  • 2
    Use `SpannableString`: http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring – pnavk Aug 16 '16 at 01:56

5 Answers5

2

I believe the SpannableString and ClickableSpan are the things you are looking for.

For more information, check this.

And Html.fromHtml is also work for this.

L. Swifter
  • 3,179
  • 28
  • 52
0

You can add an HTML <font> tag to the TextView's text.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0
  1. Use onTouch to calculate which word you just touched, look at this.
  2. And :

    TextView.setText(Html.fromHtml("assumble your html style string to change specified word color"));

Community
  • 1
  • 1
RRTW
  • 3,160
  • 1
  • 35
  • 54
0

L. Swifter is on the right track.

Here's a very simple version you can build off of.

SpannableString spannableString = new SpannableString(yourstring);

ClickableSpan clickableSpan  = new ClickableSpan() {
        boolean clicked = false;

        @Override
        public void onClick(View view) {
            clicked = true;
            view.invalidate();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            if (this.clicked) ds.setColor(Color.RED);
        }
    };

spannableString.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

textview.setText(spannableString);
textview.setMovementMethod(LinkMovementMethod.getInstance());

start and end are the index positions of "Robert"

Make sure your setText is using the spannableString, not the original string.

Have fun!

TWL
  • 6,228
  • 29
  • 65
0

Summarize the answer of L. Swifter and TML, also used the answer from select a word on a tap in TextView/EditText

The following code should be work if you click any word in the TextView. The color would be changed to red if you clicked it. Other words color will be reset to black.

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView lTextView = (TextView) findViewById(R.id.textView);
    breakEveryWord("Clickable words in text view ", lTextView);
}
...

private void breakEveryWord(String passage, final TextView pTextView) {
    String definition = passage.trim();
    pTextView.setMovementMethod(LinkMovementMethod.getInstance());
    pTextView.setText(definition, TextView.BufferType.SPANNABLE);
    final Spannable spans = (Spannable) pTextView.getText();
    BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
    iterator.setText(definition);
    int start = iterator.first();
    for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
            .next()) {
        String possibleWord = definition.substring(start, end);
        if (Character.isLetterOrDigit(possibleWord.charAt(0))) {
            ClickableSpan clickSpan = new CustomClickableSpan(possibleWord, new CallBack() {
                @Override
                public void clearAll() {
                    CustomClickableSpan[] toRemoveSpans = spans.getSpans(0, pTextView.getText().length(), CustomClickableSpan.class);
                    for (CustomClickableSpan toRemoveSpan : toRemoveSpans) {
                        toRemoveSpan.reset(pTextView);
                    }
                }
            });
            spans.setSpan(clickSpan, start, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
public static class CustomClickableSpan extends ClickableSpan {
    final String mWord;
    boolean clicked = false;
    final CallBack mCallBack;
    public CustomClickableSpan(String pWord, CallBack pCallBack) {
        mWord = pWord;
        mCallBack = pCallBack;
    }
    public void reset(View widget) {
        clicked = false;
        widget.invalidate();
    }
    @Override
    public void onClick(View widget) {
        Log.d("tapped on:", mWord);
        mCallBack.clearAll();
        clicked = true;
        widget.invalidate();
    }
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
        ds.setColor(Color.BLACK);
        if (this.clicked) ds.setColor(Color.RED);
    }
}
public interface CallBack {
     void clearAll();
}
}
Community
  • 1
  • 1
Long Ranger
  • 5,888
  • 8
  • 43
  • 72