I have a text gathered from API and it consists of custom syntax. I display it using TextView but I need to render special part of the text and convert those text into elements. An example of a text is below.
Oh, that's great idea! Check (user: someuser)'s answer for this.
You can as well look at (ref: a forum post title) or:
(ref: another forum post)
This is a hint for you! (hint: should be displayed as a mini icon
but when clicked, a tooltip should be opened)
Here is a spoiler for you.
---- spoiler ----
some spoiler. this text should not be displayed directly but it should be
converted to a button element. When the button is clicked, this text should
be shown
---- spoiler ----
So, I need to convert (user: someuser) into a link in the view which will be shown as "someuser" but when clicked, it will open a new intent. For (ref: topic) keyword, it's the same. When I hit spoiler, I want to make it a nice button indicating that it's a spoiler and when user clicks, it will expand.
How can I parse this text and show correctly in a view? I don't have problem with parsing but my problem is displaying those elements in the view.
Methods I Tried
I tried WebView instead of TextView thinking that I can convert those into HTML/Js but the performance was terrible. Although I use RecycleView and ViewHolder, WebView is simply an overkill. I need to use some kind of a TextView but with the elements I would like to have.
I also looked at custom HTML tag handling with TextView (thinking I can convert those legacy tags into html) but I failed to see how I can insert button for spoiler tag or insert custom elements like tooltip for hint.
Thank you!
I'm creating a note taking app for Android and i would like the user to be able to format their text, i.e. bold, different text sizes.
What I would like to know is how can I do this bearing in mind the text will have to be store-able in a database.
Can i use a web view with a string of HTML i pull from the database and a custom defined CSS to style it on the user end?
I also know there is some sort of formatting class in Android but im unsure of how it work, im not convinced it would be easy to store the formatting information.
I think you should use Spannable text. See android.text.*. I think you should store the text in HTML markup. Also take a look at
http://developer.android.com/guide/faq/commontasks.html#selectingtext
I am wondering if there are any good options to implement a rich text editor in Android. Please note I am talking about a rich text editor that can be used in an Android application, not the one embedded in a web page using HTML and Javascript.
My requirements are:
Basic formatting (color, fonts, highlight, bold, italic, underline, etc.)
Hyperlinks
Inline images
Bullet lists and numbered lists
Inline table (only the contents inside a cell is editable, not the table structure)
As you can see, this is pretty much something quite similar to a typical RichEdit control on Windows.
Here are some efforts (investigation & prototyping) I have made so far:
Using WebView
I have tried using a WebView control to load an HTML fragment with one . The content becomes editable and as it is HTML, I suppose it can meet most of my requirements. But it has several issues:
(deadly) No text caret. The user will have no idea where his/her typed characters will be inserted.
The on-screen soft keyboard is not visible by default. There is a trick that the user has to long-press the Menu button to bring up the keyboard. But I think this is a very bad user experience. Besides, the screen layout is not properly rearranged and the text inserting point sometimes will be covered by the keyboard.
Using EditText
I have tried using the EditText control. It seems to support some level of rich text editing (color, fonts, bold, italic, underline, inline images, bullet lists). But I still cannot figure out how I can implement the following requirements:
Control the appereance of the bullet symbol (dot, circle, dash, arrow, star, etc.)
Numbered list (1., 2., 3., etc.)
Table
BTW, I have seen there are several *Span classes out there but I am not sure if they can be any help... And the http://developer.android.com does not provide much useful information about them.
So, how on earth can I implement a rich text editor on Android?
Can I extends the EditText and add my new functionalities? Or should I do something from scratch - extends the View and implement everything by myself? For later option (extending View), I actually even don't know how to show a text caret and blink it, not mentionging moving the caret with user typing.
I am desperate now... Any hints?
Thanks!
-Tony
(EDIT)
After some further investigation, it looks like extending EditText would be my best bet. I somehow figured out how to use those Span classes and guess I should be able to do most of the tricks by using (extending) them.
For example, extending the BulletSpan and overriding drawLeadingMargin should give me the control of the bullet appereances. Extending the LeadingMarginSpan should help me on the numbered list.
As to the table, my initial plan is to extend the LineBackgroundSpan and draw all the table borders in the drawBackground override. However, I still need to figure out how to layout all the text in the table cells and properly handle the caret movement and selection. Suggestions?
I just published my rich text editor component under the Apache 2.0 license:
https://github.com/1gravity/Android-RTEditor
You can make use of any of the following libraries:-
https://github.com/commonsguy/cwac-richedit
https://github.com/chinalwb/Android-Rich-text-Editor
https://github.com/wasabeef/richeditor-android
https://github.com/irshuLx/Android-WYSIWYG-Editor
I would probably extend both EditText and TableLayout or at least end up using most of their source if there were big enough changes I needed to make.
Can you do the following:
Manually hold the contents in the EditText as your own model (ie by seperating and maintaing the content document and the view attributes as seperate entities).
Override the render (or draw method) to do custom layout on parts of the content document (part of your model) that handle non text characters (say bullets with particular attribute).
To me seems like if you have to muck about with the layout, are you better off writing it from scratch on your own. From what I remember the Edit text (and the richt text editor) is great for anything where you the data is pure text.
There is an open source EditText rich text editor called android-richtexteditor but the code was inexplicably deleted in r5. The code is still there in r4; just use svn to check out r4 or earlier to access it. The code appears to support italics, underline, color picker, text size, and more.
Also answered in these questions: 1, 2
Extend from EditText is a best choice for you, it support CharacterSpan and
ParagraphSpan.
See my App on the Google Play:
https://play.google.com/store/apps/details?id=com.hly.notes
Check this open source Wordpress mobile application for android.It has very promising Richtexteditor based on Edittext.
You can download the source from here
Thanks
When I in my android application import text from my database it returns text with a square symbol. The text in the database is pasted from a webpage so I belive it is som html formatting, [ENTER] etc.
Is it possible to make the textview understand the formatting and use it as it is intended to look in a webbrowser. I am not interested in changing the view if it can be avoided, just to make the textstring use the formatting to make \nl etc.
Any one know how it can be done.
Before setting the text in the TextView you can use the Html.fromHtml method on the text, this will format the text appropriately.
I have a large text view that I am styling with a SpannableStringBuilder. I want to be able to assign custom click event handlers to certain spans of text. For example, if I click a superscript character I want it to pop up a toast with some information about what the superscript references. So far I have found linkify which helps to make regular expression type of things like emails and phone numbers launch appropriate activities. What I want to be able to do is define a span and its styling and assign a click handler to it. I haven't found anything built in that supports this kind of functionality and so I'm asking for anyone with a fresh idea of how to do this. Thanks.
The only thing I can think of is for you to take a look at the Android source code for the Linkify class and see how that does it.
Take a look at extending ClickableSpan.
http://developer.android.com/reference/android/text/style/ClickableSpan.html
URLSpan which is the span used to Linkify, is an example implementation of ClickableSpan.
http://developer.android.com/reference/android/text/style/URLSpan.html