0
String text="Android is a operating system for mobile. Android latest version is kitkat.";

I have two edittext. the first edittext is used to search the text. The second edittext is used to display the text.

If i am entered the android in first edittext it search the String text and display the result in second edittext. Now mu question is how to replace the Android text with changing the color of Android text only.
Eg:Android(red color) is a operating system for mobile. Android(red color) latest version is kitkat.

I tried with String ss=text.replaceAll(Android font color=\"red\" + Android + "/font
this code.
edtresult.setText(Html.fromHtml(ss).
Html removes all the spaces

Thanks in advance

2 Answers 2

3

You can try Spannable like this:

EditText et = (EditText) findViewById(R.id.edit_text);
        SpannableString ss = new SpannableString("Android is a operating system for mobile. Android latest version is kitkat.");
        String textToSearch = "Android";
        Pattern pattern = Pattern.compile(textToSearch);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            ss.setSpan(new ForegroundColorSpan(Color.RED), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
et.setText(ss);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Spannables as described here

1 Comment

Though, not related to question it is a good read on spannables. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.