• Stars
    star
    310
  • Rank 134,926 (Top 3 %)
  • Language
    CSS
  • Created over 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A view for source code syntax highlighting on Android

HighlightJs View - Android

A syntax highlighting view, powered by highlight.js, for Android.

Maven Central Android Arsenal

Demo Application

The demo application shows a simple example usage for this library. It lists the files of this repository's commits and allows you to browse the changed files. Selecting a file show's up the syntax highlighted content.

Screenshot

Download

You can download the sample apk in the release section

Getting Started

Add the library as dependency

Add the library as dependency to your build.gradle file.

dependencies {
	//other dependencies...
	compile 'com.pddstudio:highlightjs-android:X.X.X'
}

Replace X.X.X with the current version - which can be found in the release section

Include the View into your Layout

Add the View to your existing layout file.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Other views here -->

    <com.pddstudio.highlightjs.HighlightJsView
	android:id="@+id/highlight_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Other views here -->

</RelativeLayout>

Assign the view and set it's content

In your Activity/Fragment assign the view and set it's content.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_syntax);
        //other stuff here ....

        //find and instantiate the view
        highlightJsView = (HighlightJsView) findViewById(R.id.highlight_view);

		//optional: register callbacks and style the view

        //register theme change listener
        highlightJsView.setOnThemeChangedListener(this);
        //change theme and set language to auto detect
        highlightJsView.setTheme(Theme.ANDROID_STUDIO);
        highlightJsView.setHighlightLanguage(Language.AUTO_DETECT);
        //load the source (can be loaded by String, File or URL)
        highlightJsView.setSource(fileObject.getUrl());
    }

Customize the View

For a complete overview of all themes and languages you can check out the official highlight.js demo here

You can customize the View's Theme and Language. By default the Theme is set to Theme.DEFAULT and the language is set to Language.AUTO_DETECT

Change the view's theme:

//change theme
highlightJsView.setTheme(Theme.ANDROID_STUDIO);

The library supports all themes that are included in highlight.js version 9.10.0

Browse the list of themes here

Change the view's language:

//change language
highlightJsView.setHighlightLanguage(Language.JAVA);

The library supports all languages that are included in highlight.js version 9.10.0 (more than +130 languages)

Browse the list of languages here

Enable or disable support for zooming:

//enable or disable zooming support for HighlightJsView
//note: in case you change this during runtime don't forget to
//call highlightJsView.reload(); for the changes to apply!
highlightJsView.setZoomSupportEnabled(true);

Enable or disable line numbers:

//enable or disable line numbers on the left side of the source
//note: in case you change this during runtime don't forget to
//call highlightJsView.reload(); for the changes to apply!
highlightJsView.setShowLineNumbers(enableLineNumbers);

By default Highlight.js does not support line numbers. However, thanks to wcoder and all the hard work that was put into highlightjs-line-numbers.js. This made it easy for me to port and include this functionality into this library.

Change the view's source

You can load the source you want to be highlighted by either a String containing the actual source code, a File object which represents the file containing the source code or an URL object to load the source code from a remote Url.

try {
	markdownUrl = new URL("https://raw.githubusercontent.com/PDDStudio/highlightjs-android/master/README.md");
} catch (Exception e) {
	e.printStackTrace();
}
//change the displayed source
highlightJsView.setSource(markdownUrl);
//make sure to call reload() when changing content during runtime
highlightJsView.reload();

Notes:

  • Source Code which is loaded via URL requires internet permission (source is loaded asynchronous) <uses-permission android:name="android.permission.INTERNET" />
  • Source Code which is loaded by a File object from the device's internal storage requires storage permission (especially on +Android 6.0)

About & Contact

License

    Copyright 2016 Patrick J

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

	Copyright (c) 2006, Ivan Sagalaev
	All rights reserved.
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	    * Neither the name of highlight.js nor the names of its contributors
	      may be used to endorse or promote products derived from this software
	      without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

More Repositories

1

EncryptedPreferences

AES-256 encrypted SharedPreferences for Android.
Java
536
star
2

earthview-android

A library to include 'EarthView with Google' into your application with ease.
Java
139
star
3

Talk

Voice integration for Android with a fluent and easy to use interface
Java
94
star
4

StarView

A space-like animated Layout for Android inspired by StarWars
Java
87
star
5

GoogleUrlShortener

An easy to use Google URL Shortener Library
Java
69
star
6

OTGSubs

Android Application to create, edit and build Substratum Themes on your Phone
Java
38
star
7

OpenPocket

[WIP] OpenSource Android Money-Management App
Java
34
star
8

NetworkUtils

A library for various network utils on Android
Java
31
star
9

tinyIFTTT

A tiny, lightweight, easy to use and customize IFTTT (if this, then that) server and (Android) client. The ideal thing for hackers and home-automation freaks.
Java
31
star
10

phrase-java

Simple & Tiny String Template Library for Android/Java
Java
15
star
11

mdl-lightdm

A Material Design LightDM Theme
CSS
14
star
12

O-Rly-Android

An Android App to generate parody O RLY Book Covers
Java
10
star
13

SimpleThemeEngine

[DEPRECATED] Simple ThemeEngine for Android, allowing you to change your Application's theme during runtime with ease.
Java
9
star
14

hyper-nord

A Nord color scheme theme for Hyper
JavaScript
7
star
15

easyflashlight

An easy to use Flashlight library using Android's Camera2 API
Java
7
star
16

themer-android

An Android colors theme generator for themer.
JavaScript
5
star
17

android-butler

[Concept] Android Library for serving different content types with ease, Sir!
Java
4
star
18

OTGSubs-Domination

A sample for OTGSubs integration based on Domination Substratum Theme by @daveyannihilation
Java
4
star
19

iw4x-docker

Experimenting with IW4X and Docker. 🐳
Shell
3
star
20

vscode-steam-vgui

Steam's VGUI Language Support & Code Snippets for Visual Studio Code
3
star
21

get-csgo-inventory

A simple npm package to query a user's CSGO inventory
TypeScript
2
star
22

ZooperUtils

Android Library for Zooper Dashboards.
Java
2
star
23

shelljs-plugin-bash-exec

A ShellJS plugin to execute commands using bash.
JavaScript
1
star
24

Share

[WIP] Android library to implement content-sharing possibility into your application with ease
Java
1
star
25

brbox-android

[WIP] Android App to communicate with the BrBox (Raspberry-Image)
Java
1
star
26

simple-request-parser

Simple version to parse the requests generated from the iconmanager library
Java
1
star
27

timefmt

πŸ•“ A tiny command line utility for time conversion
JavaScript
1
star