Giter Club home page Giter Club logo

codeeditor's Introduction

Code Editor

Download

Code Editor is an Android library that simplify the display of code, making easy syntax highlighting and showing number of lines.


Requirements

  • minSdkVersion 15
  • compileSdkVersion 28

Download

Include into the build.gradle file:

dependencies {
    implementation 'me.testica:codeeditor:1.0.2'
}

Usage

From layout:

<me.testica.codeeditor.Editor
            android:id="@+id/editor"
            app:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

Then, manipulate programmatically:

Java:

Editor editor = (Editor) findViewById(R.id.editor);
editor.setText("Hello Android");

Kotlin:

editor.setText("Hello Android")

Syntax Highlight Rules

Define a list of rules using a regular expression and color

Java:

editor.setSyntaxHighlightRules(
        new SyntaxHighlightRule("[0-9]*", "#00838f"),
        new SyntaxHighlightRule("/\\\\*(?:.|[\\\\n\\\\r])*?\\\\*/|(?<!:)//.*", "#9ea7aa")
);

Kotlin:

editor.setSyntaxHighlightRules(
        SyntaxHighlightRule("[0-9]*", "#00838f"),
        SyntaxHighlightRule("/\\\\*(?:.|[\\\\n\\\\r])*?\\\\*/|(?<!:)//.*", "#9ea7aa")
)

Keep in mind that lasts rules will overwrite the previous ones, so order is important.

Customization

From xml we can set:

  • text: text code as string
  • textSize: text dimension of text including number lines
  • fontFamily: typeface of text including number lines

The same way we can set and get above attributes programmatically:

  • setText(String)
  • setTextSize(Float)
  • setTypeface(Typeface)

Is it possible to customize the number lines or the code text separately? Yes! Programmatically we can get both widget reference and manipulate them:

  • getEditText(): returns an EditText superclass, with this we can handle the code view.
  • getNumLinesView(): returns a TextView superclass, with this we can handle the number lines view.

Below an example changing the background color of number lines view and applying some padding to code view:

Java:

// changing text color and background color to number lines view
editor.getNumLinesView().setBackgroundColor(Color.BLACK);
editor.getNumLinesView().setTextColor(Color.WHITE);

// applying left padding to code view
editor.getEditText().setPadding(10, 0, 0, 0);

Kotlin:

// changing text color and background color to number lines view
editor.getNumLinesView().apply { 
    setBackgroundColor(Color.BLACK)
    setTextColor(Color.WHITE)
}

// applying left padding to code view
editor.getEditText().setPadding(10, 0, 0, 0)

You can see another approach inside codeeditorexample/MainActivity class.

Contribute

I accept pull requests to fix, improve performance or well-situated feature! Feel free to submit one.

License MIT

MIT License

Copyright (c) 2019 Leonardo Testa

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.