CodeView - Android Syntax Highlighting Library

A high-performance Android library for real-time code syntax highlighting. Supports 20+ languages with customizable themes and minimal main-thread impact.

KotlinHiltRegexTextWatcherCustom Views

CodeView: Real-Time Syntax Highlighting for Android

Project Type: Android Open Source Library
Role: Sole Creator & Maintainer
Tech Stack: Kotlin, Android SDK, Regular Expressions, Custom Span Architectures


The Problem

Displaying code on mobile devices often suffers from two extremes: either it is a static, non-interactive block of text, or it relies on heavy, battery-draining WebViews to render HTML-based highlighting.

For developers building IDEs, technical blogs, or snippet-sharing apps on Android, there was a clear need for a native, lightweight, and performant solution that could handle real-time syntax highlighting as the user types, without dropping frames or sacrificing battery life.


Technical Architecture

I designed CodeView as a specialized AppCompatEditText subclass that intercepts text changes and applies styling dynamically using Android's Spannable system.

The core engine operates on a Lexical Analysis Layer that uses a collection of optimized Regular Expressions to tokenize the input text into keywords, literals, comments, and operators. This tokenization happens in a debounced manner to ensure that rapid typing doesn't trigger redundant re-rendering cycles.


Key Engineering Decisions

The Spannable Strategy. I chose to use SpannableStringBuilder over rebuilding the entire layout on every change. This allowed me to surgically update only the colors and styles of specific text ranges, significantly reducing the layout and measure passes required by the Android UI toolkit.

Pattern Optimization. To support 20+ languages (Kotlin, Java, Python, JavaScript, etc.), I implemented a pluggable "Language Highlighter" architecture. Each language defines its own regex patterns. To maintain performance, I optimized these patterns to avoid "catastrophic backtracking," a common pitfall in regex engines that can freeze the UI thread.

Theme Engine. I built a decoupled theme engine that maps tokens (e.g., KEYWORD, STRING) to specific color and typeface attributes. This allows developers to switch between "Dracula," "GitHub," or custom themes at runtime with a single method call.


Result and Impact

CodeView became a go-to library for Android developers needing native code rendering. It achieved:

  • 60 FPS Typing Performance: Even with large files (>1000 lines), the debounced regex engine ensures zero perceptible lag.
  • Minimal Footprint: No external dependencies, keeping the library under 100KB.
  • Community Adoption: Successfully integrated into multiple open-source Android IDEs and snippet managers on the Play Store.

Future Improvements

If I were to rebuild CodeView today, I would explore Tree-sitter for more accurate incremental parsing and utilize Jetpack Compose's VisualTransformation API for even more efficient UI state management.