Android CodeView the easiest way to highlight text

amrdeveloper

Amr Hesham

Posted on May 18, 2021

Android CodeView the easiest way to highlight text

Hi, I am Amr Hesham a Software Engineer, I am interested in Android and Compiler Development,

In my first article, I wrote about how to use the CodeView library to create a syntax highlighter for programming languages with many extra features like autocomplete, change theme and syntax in the runtime,

The first idea is to use it in Content sharing applications so for example like Twitter you can highlight hashtags, website URLs and you can also highlight emails, this feature can easily be done using CodeView in just 3 lines of code.

codeview.addSyntaxPattern(Pattern.compile("#[a-zA-z0-9]+"), Color.BLUE);
codeview.addSyntaxPattern(Patterns.WEB_URL, Color.BLUE);
codeview.addSyntaxPattern(Patterns.EMAIL_ADDRESS, Color.BLUE);
Enter fullscreen mode Exit fullscreen mode

You can also add autocomplete for email providers like @gmail.com or @yahoo.com and the final result will be like this.

Alt Text

The second idea is to use CodeView with a searching feature to highlight all matched keyword in the content and you can easily highlight without
searching in the positions in all content and add for example some HTML tags or Spans to highlight one word each time the user search for it,
and this feature can easily be done using few methods.

first, you need to create a Pattern for your search keyword, it can be a normal string or you can enable the user to search with regex, not just string.

Pattern keywordPattern = Pattern.compile(keyword);
Enter fullscreen mode Exit fullscreen mode

In Kotlin will be:

val keywordPattern : Pattern = Pattern.compile(keyword)
Enter fullscreen mode Exit fullscreen mode

then you need to add this pattern to CodeView with the color you want to highlight with it.

codeview.addSyntaxPattern(keywordPattern, Color.BLUE);
Enter fullscreen mode Exit fullscreen mode

then you need to tell CodeView to highlight the new patterns.

codeview.reHighlightSyntax();
Enter fullscreen mode Exit fullscreen mode

once the user changes the search keyword you just need to replace the old pattern with a new one and there are 2 ways to do that.

codeview.removeSyntaxPattern(keywordPattern);
Enter fullscreen mode Exit fullscreen mode

or you can remove all the patterns from CodeView

codeview.resetSyntaxPatternList();
Enter fullscreen mode Exit fullscreen mode

then you need to add the new pattern and highlight

codeview.addSyntaxPattern(newkeywordPattern, Color.BLUE);
codeview.reHighlightSyntax();
Enter fullscreen mode Exit fullscreen mode

and you have done, the final result will be like this.

Alt Text

Keep in your mind that CodeView has many features and you can use it in many different ideas I just give you 2 examples but here is more and more.

you can use it in languages apps for example English app and with an error highlighter, you can highlight spell mistake words, or create a note app with English words on autocomplete …etc.

all you need is to use your imagination and creativity to create great features with CodeView.

It’s very easy to use, well documented, has many examples on Github you can know how to download and use from the Github link:
https://github.com/amrdeveloper/codeview

and I hope you will enjoy creating your editor with CodeView 😋.

Enjoy Programming 😋.

💖 💪 🙅 🚩
amrdeveloper
Amr Hesham

Posted on May 18, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related