|
| 1 | +# CodeBlockView |
| 2 | + |
| 3 | +An Android View for displaying read-only code snippets. Extracted from |
| 4 | +[Kotlin](https://github.com/halilozel1903/Kotlin). |
| 5 | + |
| 6 | +- Kotlin syntax highlighting, line numbers and horizontal scrolling |
| 7 | +- Selectable source and a copy button that preserves the original whitespace |
| 8 | +- Light/dark palettes and English/Turkish copy labels |
| 9 | +- XML and Kotlin usage; no network access or permissions |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +Requires Android API 24+, a Material Components/Material 3 theme, and a consumer |
| 14 | +build compatible with the AndroidX/Material dependencies (compile SDK 37 for the |
| 15 | +versions used in this release). The library is built with Java 17 and AGP 9.4.0. |
| 16 | + |
| 17 | +Add the repository in `settings.gradle.kts`: |
| 18 | + |
| 19 | +```kotlin |
| 20 | +dependencyResolutionManagement { |
| 21 | + repositories { |
| 22 | + google() |
| 23 | + mavenCentral() |
| 24 | + maven("https://jitpack.io") { |
| 25 | + content { includeGroup("com.github.halilozel1903") } |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Add the versioned dependency in your module: |
| 32 | + |
| 33 | +```kotlin |
| 34 | +implementation("com.github.halilozel1903:CodeBlockView:1.0.0") |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```xml |
| 40 | +<com.halilozel.codeblockview.CodeBlockView |
| 41 | + android:id="@+id/codeBlock" |
| 42 | + android:layout_width="match_parent" |
| 43 | + android:layout_height="wrap_content" /> |
| 44 | +``` |
| 45 | + |
| 46 | +```kotlin |
| 47 | +import com.halilozel.codeblockview.CodeBlockView |
| 48 | + |
| 49 | +val codeBlock = findViewById<CodeBlockView>(R.id.codeBlock) |
| 50 | +codeBlock.setCode("val greeting = \"Hello Kotlin\"\nprintln(greeting)") |
| 51 | +// Other language labels are displayed as plain text, without Kotlin highlighting. |
| 52 | +codeBlock.setCode("SELECT * FROM users", language = "sql") |
| 53 | +``` |
| 54 | + |
| 55 | +`setCode(source, language)` replaces the snippet and resets the copy button. |
| 56 | +The default language is `kotlin`; `kt` and `kts` also enable highlighting. |
| 57 | +An empty language shows a localized “Code” label. Empty source disables copying. |
| 58 | +The tokenizer is a lightweight display helper, not a compiler or full parser. |
| 59 | +This is an Android View, not a Compose component or code editor. |
| 60 | + |
| 61 | +## Appearance and localization |
| 62 | + |
| 63 | +The host activity must use a theme derived from `Theme.MaterialComponents` or |
| 64 | +`Theme.Material3`. Colors adapt to Android's night mode. Override the `cbv_` |
| 65 | +resources in your app's `values` / `values-night` directories to customize the |
| 66 | +palette (for example `cbv_code_keyword` or `cbv_code_block_background`). |
| 67 | +Copy button labels use `cbv_code_copy`, `cbv_code_copied`, `cbv_code_clip_label` |
| 68 | +and `cbv_code_plain`; provide localized overrides for additional languages. |
| 69 | + |
| 70 | +## Build and contribute |
| 71 | + |
| 72 | +Install JDK 17 and Android SDK 37, then run: |
| 73 | + |
| 74 | +```sh |
| 75 | +./gradlew testDebugUnitTest lintRelease publishToMavenLocal |
| 76 | +``` |
| 77 | + |
| 78 | +Pull requests and issues are welcome. Keep source-copy behavior and token ranges |
| 79 | +covered by tests. CI checks tests, lint and the Maven publication. Releases use |
| 80 | +immutable version tags consumed through [JitPack](https://docs.jitpack.io/android/). |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +[MIT](LICENSE). |
0 commit comments