2

I'm working on a kind of typing game on Android. It has a Text View with the sentence the user has to type in. Each time the current word the user has to type in will be underlined, once user finished a word, that word will change color based on the time it took to type (e.g. >5 seconds = red, <5 seconds is green).

My question is how can I underline and change the color of the words in the Text View so that when I underline a new word, it will also keep the color of the previous ones?

Currently I'm doing some thing like this every time I move to the next word

String texts = myTextView.getText().toString();
// put the next word to be underlined between <u> </u> tags
myTextView.setText(Html.fromHtml(texts));

As you can see, it will underline the next word but all of the colors of the previous words will be lost but I want to keep those.

Thanks for your help!

Edit: My text is already fixed and the user has to type in the words they see. For example, I have a sentence "Sample Sentence Hello". I want "Sample" to be underlined at first, then "Sentence" underlined, "Sample" changed red or green, then "Hello" underlined and "Sample" and "Sentence" each changed to red or green depending on the time it took the user to type each word.

Nguyen
  • 25
  • 1
  • 5
  • You should go with Spannable class. http://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android – K. Gandhi Jun 21 '16 at 04:00
  • @K.Gandhi Thanks, as I see it, I have to store the start-end indices of each word to use Spannable. Is that right? – Nguyen Jun 21 '16 at 04:15
  • yes, you can go with that. It's just basic idea... you should dig and get it work more easily. – K. Gandhi Jun 21 '16 at 04:50

2 Answers2

1

Try this way to create underline

<resources>
    <string name="your_string">
        This is an <u>underline</u>.
    </string>
</resources>

Java

TextView tv = (TextView) view.findViewById(R.id.tv);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content);

OR using paint

txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0

You can set it programatically to TextView.

like in if else condition .

if(time > 5){    
   String texts = myTextView.getText().toString();
   myTextView.setText(texts);
   myTextView.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
   mTextView.settextColor(Color.parsecolor("#FF0000"));

}else {
   String texts = myTextView.getText().toString();
   myTextView.setText(texts);
   myTextView.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
   mTextView.settextColor(Color.parsecolor("#0000FF"));
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • Thanks mate but how can I underline and change the color 1 word at a time? For example, my Text View holds the sentence "Sample Sentence", I want it to first have the word "Sample" underlined and then "Sentence" underlined and "Sample" changes color to red or green. – Nguyen Jun 21 '16 at 04:12
  • @Nguyen you `text` is `static` or it can be written by `User`. – Harshad Pansuriya Jun 21 '16 at 04:16
  • @Nguyen visit this it may be help you . http://stackoverflow.com/questions/13238298/android-change-underline-color-from-an-edittext-dynamically – Harshad Pansuriya Jun 21 '16 at 04:25