0

I'm working on a book aplication. In this app user must be ably to read and highlight text. I searched a lot over internet and used webview ,textview ,plain text but with no success. How can I do this? Be able to Highlight text by every way.

Community
  • 1
  • 1

3 Answers3

0

Try this library Android TextHighlighter.

Implementations

TextView.setText() gets a parameter as Spannable not only CharacterSequence. SpannableString has a method setSpan() which allows applying styles.

See list of direct subclass form CharacterStyle https://developer.android.com/reference/android/text/style/CharacterStyle.html

  • example of giving background color and foreground color for word "Hello" in "Hello, World"
Spannable spannable = new SpannableString("Hello, World");
// setting red foreground color
ForegroundSpan fgSpan = new ForegroundColorSpan(Color.red);
// setting blue background color
BackgroundSpan bgSpan = new BackgroundColorSPan(Color.blue);

// setSpan requires start and end index
// in our case, it's 0 and 5
// You can directly set fgSpan or bgSpan, however,
// to reuse defined CharacterStyle, use CharacterStyle.wrap()
spannable.setSpan(CharacterStyle.wrap(fgSpan), 0, 5, 0);
spannable.setSpan(CharacterStyle.wrap(bgSpan), 0, 5, 0);

// apply spannableString on textview
textView.setText(spannable);

There is more answers on this link

xeoh
  • 21
  • 3
0

This works for you,

SpannableString str = new SpannableString("Highlighted. Not highlighted.");
 str.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 11, 0); 
 textView.setText(str);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Sanjay Kumaar
  • 690
  • 7
  • 17
0
/** This is the way you can use Matcher, i think you are going to type in editext and textview's text should be highlioghted so this is the way...    

 search_icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Searched_text.setVisibility(View.VISIBLE);

                   String tvt = textView_searching.getText().toString();
                    String ett = Searched_text.getText().toString();
                    SpannableStringBuilder sb = new SpannableStringBuilder(tvt);
                    Pattern p = Pattern.compile(ett, Pattern.CASE_INSENSITIVE);
                    Matcher m = p.matcher(tvt);
                    while (m.find()){
                        //String word = m.group();
                        //String word1 = notes.substring(m.start(), m.end());

                        sb.setSpan(new ForegroundColorSpan(Color.RED), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                        sb.setSpan(new BackgroundColorSpan(Color.YELLOW), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    }
                    textView_searching.setText(sb);

                }
            });
Qais
  • 1
  • 4