RichTextAndroid

Additional

Language
Java
Version
N/A
Created
Aug 2, 2017
Updated
Feb 12, 2018
Owner
Square1 (square1-io)
Contributor
robertoprato
1
Activity
Badge
Generate
Download
Source code

Promotion

RichText Library

Features

  • Display rich text using a fluent interface.
  • Support Image loading from network.
  • Parse HTML, including images and video tag into displayable content.

Download

Use Gradle:

repositories {
   jcenter()
}

dependencies {
  compile 'io.square1:richtext:x.x.x'
}

Setup a RichContentView

Add a RichContentView to an xml layout, wrap around a ScrollView to enable content scrolling.

 <ScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     <io.square1.richtextlib.ui.RichContentView
         android:padding="10dip"
         android:id="@+id/richTextView"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/hello_blank_fragment" />
 </ScrollView>

Enable Image download

Supply an instance of a class that implements UrlImageDownloader instance and can download images from the network:

       contentView.setUrlBitmapDownloader(new UrlBitmapDownloader() {

            @Override
            public void downloadImage(RemoteBitmapSpan urlBitmapSpan, Uri image) {
                    Glide.with(getActivity())
                            .load(image)
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true)
                            .into(new GlideTarget(getActivity(),urlBitmapSpan));
                }

        });

Enable click events

Supply an instance of a clicked observer to receive on click events on parts of the content:

        contentView.setOnSpanClickedObserver(new RichContentViewDisplay.OnSpanClickedObserver() {
            @Override
            public boolean onSpanClicked(ClickableSpan span) {
                String action = span.getAction();
                action = TextUtils.isEmpty(action) ? " no action" : action;
                Toast.makeText(getContext(), action, Toast.LENGTH_LONG).show();
                return true;
            }
        });

Parse and display of HTML

 String html = "<p><i>This text is italic</i></p>";
 RichTextDocumentElement element = RichTextV2.textFromHtml(context, html);
 contentView.setText(element);

Sample Fluent Interface to create formatted text

 String paragraph = getResources().getString(R.string.sample_text);
       RichTextDocumentElement element = new RichTextDocumentElement
                .TextBuilder("What is Lorem Ipsum")
                .bold()
                .color(Color.BLUE)
                .underline(true)
                .sizeChange(1.5f)
                .center()
                .newLine()
                .image("https://netdna.webdesignerdepot.com/uploads/2013/07/icons-animation.gif",10,10)
                .click("You have clicked on the image at the top!")
                .newLine()
                .append("Click the Image above")
                .font("fonts/SourceCodePro-Bold.ttf")
                .center()
                .bold()
                .sizeChange(1.5f)
                .color(Color.RED)
                .newLine()
                .append(paragraph)
                .left()
                .image("http://random-ize.com/lorem-ipsum-generators/lorem-ipsum/lorem-ipsum.jpg")
                .click("You have clicked on the  image in the middle of the text")
                .append("Click the lorem ipsum image")
                .font("fonts/GreatVibes-Regular.otf")
                .center()
                .bold()
                .sizeChange(1.5f)
                .color(Color.RED)
                .newLine()
                .video("http://html5demos.com/assets/dizzy.mp4")
                .append("It has survived not only five centuries,")
                .color(Color.GRAY)
                .sizeChange(2.0f)
                .center()
                .append("but also the leap into electronic typesetting,")
                .strikethrough(true)
                .append("remaining essentially unchanged.")
                .click("Hello you have clicked on the text")
                .bold()
                .italic()
                .build();


    contentView.setText(element);

License

RichText is licensed under the Apache v2 license:

Copyright 2017 www.square1.io

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.