Handling custom tags in Android using Html.TagHandler()

While working on my app Android app Ken, I needed to render HTML in Android, I was very much delighted when I found out that android’s TextView supports HTML rendering and it can be easily achived by converting html into Spannable using Html.fromHtml( method and sending it to TextView’s setText method.

But there is catch with Html.fromHtml method, it doesn’t support all tags; well that was a killjoy, but html.fromHtml method accpets a TagHandler which is class we can implement to handle custom html tags, but it is not well documented, after struggling for a while and reading the source code of HTML class method I was finally able to define my own tagHandler class which is able to handle ul, ol and code tags.

Handling Html

Here is my custom tagHandler:

The above Java file HtmlTagHandler handles, ul, ol, and code tags.
The handleTagList() method handles the list tags, and handling of code tag was pretty easy, I just need to find the start of the code tag, I do this by adding a flag on the start of the tag Spannable.SPAN_MARK_MARK and when the tag on end I just find the object where I marked Spannable.SPAN_MARK_MARK using getLast() and find its position, and store it in the variable name “where”; that’s the start of the code tag, and I get the end of the code tag using output.length() and set the font face of that text fragment to “monospace” using (new TypefaceSpan(“monospace”).

That’s it! If you want to use it in your project create a java class with content of this file and create a object of HtmlTagHandler and pass it to your fromHtml method.

Also try out my app Ken which is available on Google Play which a free magazine for programmers, you have to select your interest (e.g Javascript, Android, Ruby etc.) and it will show you all the latest news, articles, tutorials related to your interests.

P.S: For handling image I using a nice library called “URLImageParser” Github link

Use your Android Phone as a display for Raspberrypi

I read this tutorial on how to use your Kindle as a display for your Raspberrypi link, and I thought why not use an Android phone as display and use usb ports on the Raspberry Pi for keyboard and mouse.

Things you’ll need:

  1. Android phone
  2. A Raspberry pi

What we’ll do:

  1. We will install VNC on Android, I am using pocketvnc
  2. Then, we will use USB tethering on Android and connect it to Raspberry pi using USB
  3. Then in the Raspberry pi we will install x11vnc (sudo apt-get install x11vnc) so that we can connect to an exisiting X-Window server and share it using vnc, so that we can use keyboard and mouse connected to raspberry pi.
  4. Change the ip address of raspbery pi sudo ifconfig usb0 (Because android assing IP in USB tether mode in the range 192.168.42.x)
  5. Start the X-Server (if you have not started it alerady) using the startx command.
  6. The share it using x11vnc, type the following command in the terminal: ***sudo x11vnc -display : 0
  7. Then connect to your raspberry pi to Android using the VNC software you have downloaded: port 5900 and IP

I know it is not very detialed tutorial and a bit crude, I will soon post a detialed one with images and video.
Also we could automate a lot stuff on startup using .bash_profile so that we don’t have to type in these commands everytime.
I’ll post all this in the next update.

I would love to hear about you suggestions in the comments.

(Please excuse the typos, I am very excited about this and I want to post it online ASAP, so I am not profreading it.)