|
| 1 | +# Code Editor |
| 2 | +[  ](https://bintray.com/testica-android/maven/codeeditor/_latestVersion) |
| 3 | + |
| 4 | +Code Editor is an Android library that simplify the display of code, making easy syntax highlighting and showing number of lines. |
| 5 | + |
| 6 | +## Requirements |
| 7 | +- minSdkVersion 15 |
| 8 | +- compileSdkVersion 28 |
| 9 | + |
| 10 | +## Download |
| 11 | + |
| 12 | +Include into the build.gradle file: |
| 13 | + |
| 14 | +```groovy |
| 15 | +dependencies { |
| 16 | + implementation 'me.testica:codeeditor:1.0.0' |
| 17 | +} |
| 18 | +``` |
| 19 | +## Usage |
| 20 | + |
| 21 | +From layout: |
| 22 | +```xml |
| 23 | +<me.testica.codeeditor.Editor |
| 24 | + android:id="@+id/editor" |
| 25 | + app:textSize="16sp" |
| 26 | + android:layout_width="match_parent" |
| 27 | + android:layout_height="match_parent"/> |
| 28 | +``` |
| 29 | + |
| 30 | +Then, manipulate programmatically: |
| 31 | + |
| 32 | +Java: |
| 33 | +```java |
| 34 | +Editor editor = (Editor) findViewById(R.id.editor); |
| 35 | +editor.setText("Hello Android"); |
| 36 | +``` |
| 37 | +Kotlin: |
| 38 | +```kotlin |
| 39 | +editor.setText("Hello Android") |
| 40 | +``` |
| 41 | + |
| 42 | +### Syntax Highlight Rules |
| 43 | + |
| 44 | +Define a list of rules using a regular expression and color |
| 45 | + |
| 46 | +Java: |
| 47 | +```java |
| 48 | +editor.setSyntaxHighlightRules( |
| 49 | + new SyntaxHighlightRule("[0-9]*", "#00838f"), |
| 50 | + new SyntaxHighlightRule("/\\\\*(?:.|[\\\\n\\\\r])*?\\\\*/|(?<!:)//.*", "#9ea7aa") |
| 51 | +); |
| 52 | +``` |
| 53 | +Kotlin: |
| 54 | +```kotlin |
| 55 | +editor.setSyntaxHighlightRules( |
| 56 | + SyntaxHighlightRule("[0-9]*", "#00838f"), |
| 57 | + SyntaxHighlightRule("/\\\\*(?:.|[\\\\n\\\\r])*?\\\\*/|(?<!:)//.*", "#9ea7aa") |
| 58 | +) |
| 59 | +``` |
| 60 | +**Keep in mind that lasts rules will overwrite the previous ones, so order is important.** |
| 61 | + |
| 62 | +### Customization |
| 63 | + |
| 64 | +From **xml** we can set: |
| 65 | +- `text`: text code as string |
| 66 | +- `textSize`: text dimension of text including number lines |
| 67 | +- `fontFamily`: typeface of text including number lines |
| 68 | + |
| 69 | +The same way we can set and get above attributes programmatically: |
| 70 | +- `setText(String)` |
| 71 | +- `setTextSize(Float)` |
| 72 | +- `setTypeface(Typeface)` |
| 73 | + |
| 74 | +Is it possible to customize the number lines or the code text separately? **Yes!** |
| 75 | +Programmatically we can get both widget reference and manipulate them: |
| 76 | + |
| 77 | +- `getEditText()`: returns an `EditText` superclass, with this we can handle the code view. |
| 78 | +- `getNumLinesView()`: returns a `TextView` superclass, with this we can handle the number lines view. |
| 79 | + |
| 80 | +Below an example changing the background color of number lines view and applying some padding to code view: |
| 81 | + |
| 82 | +Java: |
| 83 | +```java |
| 84 | +// changing text color and background color to number lines view |
| 85 | +editor.getNumLinesView().setBackgroundColor(Color.BLACK); |
| 86 | +editor.getNumLinesView().setTextColor(Color.WHITE); |
| 87 | + |
| 88 | +// applying left padding to code view |
| 89 | +editor.getEditText().setPadding(10, 0, 0, 0); |
| 90 | +``` |
| 91 | + |
| 92 | +Kotlin: |
| 93 | +```kotlin |
| 94 | +// changing text color and background color to number lines view |
| 95 | +editor.getNumLinesView().apply { |
| 96 | + setBackgroundColor(Color.BLACK) |
| 97 | + setTextColor(Color.WHITE) |
| 98 | +} |
| 99 | + |
| 100 | +// applying left padding to code view |
| 101 | +editor.getEditText().setPadding(10, 0, 0, 0) |
| 102 | +``` |
| 103 | + |
| 104 | +You can see another approach inside **codeeditorexample/MainActivity** class. |
| 105 | + |
| 106 | +## Contribute |
| 107 | + |
| 108 | +I accept pull requests to fix, improve performance or well-situated feature! Feel free to submit one. |
| 109 | + |
| 110 | +## License MIT |
| 111 | +``` |
| 112 | +MIT License |
| 113 | +
|
| 114 | +Copyright (c) 2019 Leonardo Testa |
| 115 | +
|
| 116 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 117 | +of this software and associated documentation files (the "Software"), to deal |
| 118 | +in the Software without restriction, including without limitation the rights |
| 119 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 120 | +copies of the Software, and to permit persons to whom the Software is |
| 121 | +furnished to do so, subject to the following conditions: |
| 122 | +
|
| 123 | +The above copyright notice and this permission notice shall be included in all |
| 124 | +copies or substantial portions of the Software. |
| 125 | +
|
| 126 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 127 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 128 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 129 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 130 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 131 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 132 | +SOFTWARE. |
| 133 | +``` |
0 commit comments