Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.FabianTerhorst:codeview-android:1.1.2.1'
}
dependencies {
implementation("com.github.FabianTerhorst:codeview-android:1.1.2.1")
}
<dependency>
<groupId>com.github.FabianTerhorst</groupId>
<artifactId>codeview-android</artifactId>
<version>1.1.2.1</version>
</dependency>
libraryDependencies += "com.github.FabianTerhorst" % "codeview-android" % "1.1.2.1"
:dependencies [[com.github.FabianTerhorst/codeview-android "1.1.2.1"]]
CodeView helps to show code content with syntax highlighting in native way.
CodeView contains 3 core parts to implement necessary logic:<br>
<b>CodeClassifier</b> is trying to define what language presented in code snippet. It built upon Naive Bayes classifier. There is no need to work with this class directly & you must just follow instructions below. (Experimental module, may not work properly!)<br>
For highlighting it uses <b>CodeHighlighter</b>, just highlights your code & returns formatted content. It based on Google Prettify and their Java implementation & fork.<br>
<b>CodeView</b> & related abstract adapter to provide customization (see below).<br>
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add the dependency:
compile 'com.github.softwee:codeview-android:1.1.2'
If you want to use code classifier to auto language recognizing just add to your Application.java:
// train classifier on app start
CodeProcessor.init(this);
Add view for your layout:
<io.github.kbiakov.codeview.CodeView
android:id="@+id/code_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Use chaining syntax when build view:
CodeView codeView = (CodeView) findViewById(R.id.code_view);
codeView.highlightCode("js")
.setColorTheme(ColorTheme.SOLARIZED_LIGHT.withBgContent(myColor))
.setCodeContent(getString(R.string.listing_js));
And perform actions sequentially when view built:
codeView.setCodeContent(getString(R.string.listing_java));
codeView.highlightCode("java");
You can use both forms for build & built view, but note: setCodeContent(String) is final step when you build your view, otherwise not. If you firstly highlight and then set code content, code will not be highlighted if view was not built yet. Instructions above helps you to avoid errors. View has state to handle this behavior.
Use implicit form to code highlighting:
codeView.highlightCode();
or eplixit (see available extensions below):
codeView.highlightCode("js"); // it will work fast!
Use default color theme:
codeView.setColorTheme(ColorTheme.SOLARIZED_LIGHT);
or extend default:
int myColor = ContextCompat.getColor(this, R.color.my_color);
codeView.setColorTheme(ColorTheme.MONOKAI.withBgContent(myColor));
or provide your own (don't forget to open PR with this stuff!)
codeView.setColorTheme(new ColorThemeData(new SyntaxColors(...), ...));
Handle user clicks on code lines:
codeView.setCodeListener(new OnCodeLineClickListener() {
@Override
public void onCodeLineClicked(int n, @NotNull String line) {
Log.i("ListingsActivity", "On " + (n + 1) + " line clicked");
}
});
Enable shadows to hide scrolled content:
codeView.setShadowsEnabled(true);
Sometimes you may want to add some content under line. You can create your own implementation as follows:
MyModel class.<br>AbstractCodeAdapter<MyModel> typed by your model class.<br>MyCodeAdapter:// Kotlin
class MyCodeAdapter : AbstractCodeAdapter<MyModel> {
constructor(context: Context, content: String) : super(context, content)
override fun createFooter(context: Context, entity: MyModel, isFirst: Boolean) =
/* init & return your view here */
}
// Java
public class MyCodeAdapter extends AbstractCodeAdapter<MyModel> {
public CustomAdapter(@NotNull Context context, @NotNull String content) {
// @see params in AbstractCodeAdapter
super(context, content, true, 10, context.getString(R.string.show_all), null);
}
@NotNull
@Override
public View createFooter(@NotNull Context context, CustomModel entity, boolean isFirst) {
return /* your initialized view here */;
}
}
<br>
4. Set custom adapter to your code view:
```java
final MyCodeAdapter adapter = new MyCodeAdapter(this, getString(R.string.listing_py));
codeView.setAdapter(diffsAdapter);
```
<br>
5. Init footer entities to provide mapper from your view to model:
```java
// it will add an addition diff to code line
adapter.addFooterEntity(16, new MyModel(getString(R.string.py_addition_16), true));
// and this a deletion diff
adapter.addFooterEntity(11, new MyModel(getString(R.string.py_deletion_11), false));
```
<br>
6. You can also add a multiple diff entities:
```java
AbstractCodeAdapter<MyModel>.addFooterEntities(HashMap<Int, List<MyModel>> myEntities)
```
Here 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.
<br>
See Github diff as example of my "best practice" implementation.
See <a href="https://github.com/Softwee/codeview-android/blob/master/example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java">example</a>.<br>
C/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"
Didn't found yours? Please, open issue to show your interest & I try to add this language in next releases.
Copyright (c) 2016 Softwee
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.