{"id":19811737,"url":"https://github.com/kbiakov/codeview-android","last_synced_at":"2025-04-04T06:07:25.201Z","repository":{"id":45737413,"uuid":"66310925","full_name":"kbiakov/CodeView-Android","owner":"kbiakov","description":"Display code with syntax highlighting :sparkles: in native way.","archived":false,"fork":false,"pushed_at":"2022-01-16T06:29:12.000Z","size":1511,"stargazers_count":867,"open_issues_count":21,"forks_count":107,"subscribers_count":19,"default_branch":"master","last_synced_at":"2024-10-29T15:01:22.012Z","etag":null,"topics":["android","android-library","android-ui","codeview","custom-view","customview","highlighting","java","kotlin","kotlin-android","naive-bayes-classifier","recyclerview","recyclerview-adapter","syntax-highlighting"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kbiakov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-22T21:54:03.000Z","updated_at":"2024-10-28T14:41:51.000Z","dependencies_parsed_at":"2022-07-22T01:48:20.333Z","dependency_job_id":null,"html_url":"https://github.com/kbiakov/CodeView-Android","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbiakov%2FCodeView-Android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbiakov%2FCodeView-Android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbiakov%2FCodeView-Android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbiakov%2FCodeView-Android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kbiakov","download_url":"https://codeload.github.com/kbiakov/CodeView-Android/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247128746,"owners_count":20888235,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","android-ui","codeview","custom-view","customview","highlighting","java","kotlin","kotlin-android","naive-bayes-classifier","recyclerview","recyclerview-adapter","syntax-highlighting"],"created_at":"2024-11-12T09:27:37.792Z","updated_at":"2025-04-04T06:07:25.184Z","avatar_url":"https://github.com/kbiakov.png","language":"Kotlin","readme":"# CodeView (Android)\n\n[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-codeview--android-blue.svg)](https://android-arsenal.com/details/1/4216)\n[![Release](https://jitpack.io/v/kbiakov/CodeView-Android.svg)](https://jitpack.io/#kbiakov/CodeView-Android)\n[![Build Status](https://travis-ci.org/kbiakov/CodeView-android.svg?branch=master)](https://travis-ci.org/kbiakov/CodeView-Android)\n\u003ca href=\"http://www.methodscount.com/?lib=com.github.kbiakov%3ACodeView-android%3A1.3.0\"\u003e\u003cimg src=\"https://img.shields.io/badge/Methods and size-core: 969 | 1396 KB-e91e63.svg\"/\u003e\u003c/a\u003e\n\n\u003cb\u003eCodeView\u003c/b\u003e helps to show code content with syntax highlighting in native way.\n\n## Description\n\u003cb\u003eCodeView\u003c/b\u003e contains 3 core parts to implement necessary logic:\u003cbr\u003e\n\n1. \u003cb\u003eCodeView\u003c/b\u003e \u0026 related abstract adapter to provide options \u0026 customization (see below).\u003cbr\u003e\n\n2. For highlighting it uses \u003cb\u003eCodeHighlighter\u003c/b\u003e, it highlights your code \u0026 returns formatted content. It's based on [Google Prettify](https://github.com/google/code-prettify) and their Java implementation \u0026 [fork](https://github.com/google/code-prettify).\u003cbr\u003e\n\n3. \u003cb\u003eCodeClassifier\u003c/b\u003e is trying to define what language is presented in the code snippet. It's built using [Naive Bayes classifier](https://en.wikipedia.org/wiki/Naive_Bayes_classifier) upon found open-source [implementation](https://github.com/ptnplanet/Java-Naive-Bayes-Classifier), which I rewrote in Kotlin. There is no need to work with this class directly \u0026 you must just follow instructions below. (Experimental module, may not work properly!)\u003cbr\u003e\n\n## Download\nAdd it in your root ```build.gradle``` at the end of repositories:\n```groovy\nallprojects {\n    repositories {\n        ...\n        maven { url \"https://jitpack.io\" }\n    }\n}\n```\n\nAdd the dependency:\n```groovy\ncompile 'com.github.kbiakov:CodeView-Android:1.3.2'\n```\n\n## Usage\nIf you want to use code classifier to auto language recognizing just add to your ```Application.java```:\n```java\n// train classifier on app start\nCodeProcessor.init(this);\n```\n\nHaving done ones on app start you can classify language for different snippets even faster, because the algorithm needs time for training on sets for the presented listings of the languages which the library has.\n\nAdd view to your layout \u0026 bind as usual:\n```xml\n\u003cio.github.kbiakov.codeview.CodeView\n\tandroid:id=\"@+id/code_view\"\n\tandroid:layout_width=\"wrap_content\"\n\tandroid:layout_height=\"wrap_content\" /\u003e\n```\n```java\nCodeView codeView = (CodeView) findViewById(R.id.code_view);\n```\n\nSo now you can set code using implicit form:\n```java\n// auto language recognition\ncodeView.setCode(getString(R.string.listing_js));\n```\n\nOr explicit (see available extensions below):\n```java\n// will work faster!\ncodeView.setCode(getString(R.string.listing_py), \"py\");\n```\n\n## Customization\nWhen you call ```setCode(...)``` the view will be prepared with the default params if the view was not initialized before. So if you want some customization, it can be done using the options and/or adapter.\n\n### Initialization\nYou can initialize the view with options:\n```java\ncodeView.setOptions(Options.Default.get(this)\n    .withLanguage(\"python\")\n    .withCode(R.string.listing_py)\n    .withTheme(ColorTheme.MONOKAI));\n```\n\nOr using adapter (see \u003cb\u003eAdapter\u003c/b\u003e or example for more details):\n```java\nfinal CustomAdapter myAdapter = new CustomAdapter(this, getString(R.string.listing_md));\ncodeView.setAdapter(myAdapter);\n```\n\n\u003cb\u003eNote:\u003c/b\u003e Each \u003cb\u003eCodeView\u003c/b\u003e has a adapter and each adapter has options. When calling ```setOptions(...)``` or ```setAdapter(...)``` the current adapter is \"flushed\" with the current options. If you want to save the state and just update options saving adapter or set adapter saving options you must call ```updateOptions(...)``` or ```updateAdapter(...)``` accordingly.\n\n### Options\nOptions helps to easily set necessary params, such as code \u0026 language, color theme, font, format, shortcut params (max lines, note) and code line click listener. Some params are unnecessary.\n\nWhen the view is initialized (options or adapter are set) you can manipulate the options in various ways:\n```java\ncodeView.getOptions()\n    .withCode(R.string.listing_java)\n    .withLanguage(\"java\")\n    .withTheme(ColorTheme.MONOKAI);\n```\n\n### Color theme\nThere are some default themes (see full list below):\n```java\ncodeView.getOptions().setTheme(ColorTheme.SOLARIZED_LIGHT);\n```\n\nBut you can build your own from a existing one:\n```java\nColorThemeData myTheme = ColorTheme.SOLARIZED_LIGHT.theme()\n    .withBgContent(android.R.color.black)\n    .withNoteColor(android.R.color.white);\n\ncodeView.getOptions().setTheme(myTheme);\n```\n\nOr create your own from scratch (don't forget to open PR with this stuff!):\n```java\nColorThemeData customTheme = new ColorThemeData(new SyntaxColors(...), ...);\ncodeView.getOptions().setTheme(customTheme);\n```\n\n### Font\nSet font for your code content:\n```java\ncodeView.getOptions().withFont(Font.Consolas);\n```\n\n`Font.Consolas` is a font preset (see the list of available below).\nTo use your own font you can use similar method by providing `Typeface` or font path. Fonts are internally cached.\n\n### Format\nManage the space that code line take. There are 3 types: `Compact`, `ExtraCompact` and `Medium`.\nSetup is similar:\n```kotlin\n// Kotlin\ncodeView.getOptions().withFont(Font.Compact)\n```\n```java\n// Java\ncodeView.getOptions().withFont(Format.Default.getCompact());\n```\nAlso you can create custom `Format` by providing params such as `scaleFactor`, `lineHeight`, `borderHeight` (above first line and below last) and `fontSize`.\n\n### Adapter\nSometimes you may want to take code lines under your control, and that's why you need a \u003cb\u003eAdapter\u003c/b\u003e.\n\nYou can create your own implementation as follows:\n\n1. Create your model to store data, for example some ```MyModel``` class.\u003cbr\u003e\n2. Extend ```AbstractCodeAdapter\u003cMyModel\u003e``` typed by your model class.\u003cbr\u003e\n3. Implement necessary methods in obtained ```MyCodeAdapter```:\n```kotlin\n// Kotlin\nclass MyCodeAdapter : AbstractCodeAdapter\u003cMyModel\u003e {\n    constructor(context: Context, content: String) : super(context, content)\n\n    override fun createFooter(context: Context, entity: MyModel, isFirst: Boolean) =\n        /* init \u0026 return your view here */\n}\n```\n```java\n// Java\npublic class MyCodeAdapter extends AbstractCodeAdapter\u003cMyModel\u003e {\n    public CustomAdapter(@NotNull Context context, @NotNull String content) {\n    \t// @see params in AbstractCodeAdapter\n        super(context, content, true, 10, context.getString(R.string.show_all), null);\n    }\n\n    @NotNull\n    @Override\n    public View createFooter(@NotNull Context context, CustomModel entity, boolean isFirst) {\n        return /* your initialized view here */;\n    }\n}\n```\n\u003cbr\u003e\n\n4. Set custom adapter to your code view:\n```java\nfinal MyCodeAdapter adapter = new MyCodeAdapter(this, getString(R.string.listing_py));\ncodeView.setAdapter(diffsAdapter);\n```\n\u003cbr\u003e\n\n5. Init footer entities to provide mapper from your model to view:\n```java\n// it will add an addition diff to code line\nadapter.addFooterEntity(16, new MyModel(getString(R.string.py_addition_16), true));\n// and this a deletion diff\nadapter.addFooterEntity(11, new MyModel(getString(R.string.py_deletion_11), false));\n```\n\u003cbr\u003e\n\n6. You can also add a multiple diff entities:\n```java\nAbstractCodeAdapter\u003cMyModel\u003e.addFooterEntities(HashMap\u003cInt, List\u003cMyModel\u003e\u003e myEntities)\n```\nHere you must provide a map from code line numbers (started from 0) to list of line entities. It will be mapped by adapter to specified footer views.\n\u003cbr\u003e\n\nSee [Github diff](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/adapters/CodeWithDiffsAdapter.kt) as example of my \"best practice\" implementation.\n\n## How it looks in app\nSee \u003ca href=\"https://github.com/Softwee/codeview-android/blob/master/example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java\"\u003eexample\u003c/a\u003e.\u003cbr\u003e\n\n![CodeView_Android_Solarized_light](https://preview.ibb.co/daDqw8/Screen_Shot_2018_07_19_at_19_25_00.png)\n\n## List of available languages \u0026 their extensions\nC/C++/Objective-C (```\"c\"```, ```\"cc\"```, ```\"cpp\"```, ```\"cxx\"```, ```\"cyc\"```, ```\"m\"```), C# (```\"cs\"```), Java (```\"java\"```), Bash (```\"bash\"```, ```\"bsh\"```, ```\"csh\"```, ```\"sh\"```), Python (```\"cv\"```, ```\"py\"```, ```\"python\"```), Perl (```\"perl\"```, ```\"pl\"```, ```\"pm\"```), Ruby (```\"rb\"```, ```\"ruby\"```), JavaScript (```\"javascript\"```, ```\"js\"```), CoffeeScript (```\"coffee\"```), Rust (```\"rc\"```, ```\"rs\"```, ```\"rust\"```), Appollo (```\"apollo\"```, ```\"agc\"```, ```\"aea\"```), Basic (```\"basic\"```, ```\"cbm\"```), Clojure (```\"clj\"```), Css (```\"css\"```), Dart (```\"dart\"```), Erlang (```\"erlang\"```, ```\"erl\"```), Go (```\"go\"```), Haskell (```\"hs\"```), Lisp (```\"cl\"```, ```\"el\"```, ```\"lisp\"```, ```\"lsp\"```, ```\"scm\"```, ```\"ss\"```, ```\"rkt\"```), Llvm (```\"llvm\"```, ```\"ll\"```), Lua (```\"lua\"```), Matlab (```\"matlab\"```), ML (OCaml, SML, F#, etc) (```\"fs\"```, ```\"ml\"```), Mumps (```\"mumps\"```), N (```\"n\"```, ```\"nemerle\"```), Pascal (```\"pascal\"```), R (```\"r\"```, ```\"s\"```, ```\"R\"```, ```\"S\"```, ```\"Splus\"```), Rd (```\"Rd\"```, ```\"rd\"```), Scala (```\"scala\"```), SQL (```\"sql\"```), Tex (```\"latex\"```, ```\"tex\"```), VB (```\"vb\"```, ```\"vbs\"```), VHDL (```\"vhdl\"```, ```\"vhd\"```), Tcl (```\"tcl\"```), Wiki (```\"wiki.meta\"```), XQuery (```\"xq\"```, ```\"xquery\"```), YAML (```\"yaml\"```, ```\"yml\"```), Markdown (```\"md\"```, ```\"markdown\"```), formats (```\"json\"```, ```\"xml\"```, ```\"proto\"```), ```\"regex\"```\n\nDidn't found yours? Please, open issue to show your interest \u0026 I'll try to add this language in next releases.\n\n## List of available themes\n* Default (simple light theme)\n* [Solarized Light](http://www.eclipsecolorthemes.org/?view=theme\u0026id=1013)\n* [Monokai](http://www.eclipsecolorthemes.org/?view=theme\u0026id=386)\n\n## List of available fonts\n* Consolas\n* CourierNew\n* DejaVuSansMono\n* DroidSansMonoSlashed\n* Inconsolata\n* Monaco\n\n## Used by\nList of apps on Play Store where this library used. Ping me if you want to be here too!\n\nIcon | Application\n------------ | -------------\n\u003cimg src=\"https://goo.gl/ijEiBp\" width=\"48\" height=\"48\" /\u003e | [GeekBrains]\n\u003cimg src=\"https://goo.gl/1AGK1b\" width=\"48\" height=\"48\" /\u003e | [Codify - Codes On The Go]\n\u003cimg src=\"https://goo.gl/jcGMmS\" width=\"48\" height=\"48\" /\u003e | [C Programming - 200+ Offline Tutorial and Examples]\n\u003cimg src=\"https://goo.gl/4VhHbn\" width=\"48\" height=\"48\" /\u003e | [Awesome Android - UI Libraries]\n\u003cimg src=\"https://goo.gl/UPsE9F\" width=\"48\" height=\"48\" /\u003e | [GitJourney for GitHub]\n\u003cimg src=\"https://goo.gl/kNqAc7\" width=\"48\" height=\"48\" /\u003e | [Source Code - Lập Trình]\n\n## Contribute\n1. You can add your theme (see [ColorTheme](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt) class). Try to add some classic color themes or create your own if it looks cool. You can find many of them in different open-source text editors.\u003cbr\u003e\n2. If you are strong in regex, add missed language as shown [here](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/highlight/prettify/lang/LangScala.java). You can find existing regex for some language in different sources of libraries, which plays the same role.\u003cbr\u003e\n3. Various adapters also welcome.\n\n## Author\n### [Kirill Biakov](https://github.com/kbiakov)\n\n## License MIT\n```\nCopyright (c) 2016 Kirill Biakov\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n[GeekBrains]:https://play.google.com/store/apps/details?id=ru.geekbrains\n[Codify - Codes On The Go]:https://play.google.com/store/apps/details?id=com.femindharamshi.spa\n[C Programming - 200+ Offline Tutorial and Examples]:https://play.google.com/store/apps/details?id=com.rsd.cprogramming\n[Awesome Android - UI Libraries]:https://play.google.com/store/apps/details?id=in.sumeetlubal.aweandroid.aweandroid\n[GitJourney for GitHub]:https://play.google.com/store/apps/details?id=com.oklab.githubjourney\n[Source Code - Lập Trình]:https://play.google.com/store/apps/details?id=com.noah.truongpq.codeview\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkbiakov%2Fcodeview-android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkbiakov%2Fcodeview-android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkbiakov%2Fcodeview-android/lists"}