A library for displaying/editing rich text in Android.
This framework is under development and may contain bugs and limitations.
This library provides ready to use widgets, RichTextView
and RichEditText
, for diplaying and editing rich text. It also has classes to add rich text feature to any TextView
or EditText
.
The class RichTexter
enables us to add rich text support to any TextView
. Pass the TextView
instance to this class and the methods getHtml()
and setHtml()
allows us to get and set html source to the text view.
The class RichEditTexter
, a subclass of RichTexter
, enables rich text support to any EditText
. Pass the EditText
instance to this class and use the methods apply...()
or remove...()
to apply or remove markups. This class also contains a method called onMarkupMenuClicked()
which we can call when a markup menu (like Bold, Italic) is clicked. This method takes care of whether to apply or remove or toggle the markup. This will be useful when we implement our own menu.
Currently supported markups are
- Bold
- Italic
- Underline
- Strikethrough
- Link
- Font (typeface, size, color, background color)
- Ordered List
- Unordered List
- Paragraph (alignment, top spacing, bottom spacing)
- Superscript
- Subscript
- Code
- Code Block
Note
There is no exact analogy between the markups of this framework and html tags. For example Paragraph markup supports top and bottom spacing but html's <p>
don't. Hence while converting to and from html some styling may be discarded or lost. Sometimes the same markups could be used for different styles. For exampe both the foreground and background color styles are achieved using Font markup.
Current Limitations
- No support for full justification in Paragraph markup.
- Will misbehave when aready styed text is altered using autocompete dictionary suggestions. Either disable the dictionary suggestion or apply styles on competed text.
This framework can be extended to support new markups (which are achievable using Android's spans) with minimal effort. To do so
- create a class extending
Markup
(or one of its subclasses) and override the abstract methods, - add a markup menu for the newly created markup,
- on its click call
RichEditText.onMarkupMenuClicked()
by passing necessary params and the framework will take care of rest.
If you want the new markup to participate in html format conversion then do the following.
- While calling
RichTextView.setHtml()
pass a factory which returns the class of new markup for the new html tag. (You can useHtmlUtil.defaultMarkupFactory
which returns the default markup classes for defaultly supported html tags.) - Implement an
MarkupConverter.UnknownMarkupHandler
and pass it while callingRichTextView.getHtml()
.