Click here to Skip to main content
15,908,776 members
Articles / Mobile Apps / Android

Android CodeView: Auto Indentation, Find and Replace

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Jan 2022CPOL2 min read 4K   1   2
Create Android Code Editor app with CodeView that supports auto indenting, finding matching and replacing keywords
This article discusses how to support auto-indentation, and how to add support for finding matching and replacing keywords, both of which are features from the newest version of the CodeView library.


In this article, I will talk about two features from the newest version of the CodeView library - support auto-indentation, and add support for finding matching and replacing keywords.

Note that you can find the full documentation about how to install and use CodeView in the AmrDeveloper/CodeView official Repository.

So First, What is Auto Indentation?

Auto indentation helps you to set the level of indentation when you type new text, indentation level will automatically increase or decrease by the tab length, for example by 4, when you insert new lines in the end or in the middle of the text.

For example, in the modern IDE, when you write Java when you start writing new multi-lines block with {, the indentation level will increase by tab length, for example 4 and once you end this block with }, the indentation will decrease.

To support this feature in CodeView, you can do it only in three steps.

Step 1

Set the indentations start characters, so when the user types those characters, the indentation level should increase for example `{`.

Set<Character> indentationStart = new HashSet<>();

Step 2

Set the indentations to end characters, so when the user types those characters, the indentation level should decrease, for example `}`.

Set<Character> indentationEnds = new HashSet<>();

Step 3

Set the tab length and enable the auto indenting.


You can easily change the highlighting color:


The second feature is inspired by a Feature suggestion from a user of CodeView, which is the ability to highlight and get the matched substring and also replace it with another text easily, this feature you can also see in the modern Code Editors and IDEs like VS Code, Android Studio, etc.

So now, CodeView provides some methods to help you implement Find and Replace dialog.

To get all the matched substrings from the text, you can use findMatches.

List<Token> tokens = codeView.findMatches(regex);

To Find and highlight the next matching token, returns null if not found.

Token token = codeView.findNextMatch();

To find and highlight the previous matching token, returns null if not found.

Token token = codeView.findPrevMatch();

To clear all matching highlighted tokens.


To replace the first string that matches regex with another string.

codeView.replaceFirstMatch(regex, replacement);

To replace all strings that match regex with other string.

codeView.replaceAllMatches(regex, replacement);

In the example app on Github, you will find a dialog that uses this feature to support find and replacement:

You can find me on GitHub.

Enjoy programming 😋.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Junior)
Egypt Egypt
Software Engineer who able to build high-performance maintainable Software tools and applications with a modern and fast UI to provide a great experience for the user, I will be very interested to work on a unique project with new challenges to push my limit and learn new things.

Comments and Discussions

GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA12-Jan-22 4:54
professionalȘtefan-Mihai MOGA12-Jan-22 4:54 
GeneralRe: My vote of 5 Pin
AmrDeveloper13-Jan-22 8:08
AmrDeveloper13-Jan-22 8:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.