{"id":17,"date":"2013-06-11T18:30:00","date_gmt":"2013-06-11T18:30:00","guid":{"rendered":"\/blog\/blog\/Handling_Custom_Tags_Using_Html.tagHandler().html"},"modified":"2013-06-11T18:30:00","modified_gmt":"2013-06-11T18:30:00","slug":"handling-custom-tags-in-android-using-html-taghandler","status":"publish","type":"post","link":"https:\/\/mohammedlakkadshaw.com\/blog\/handling-custom-tags-in-android-using-html-taghandler.html","title":{"rendered":"Handling custom tags in Android using Html.TagHandler()"},"content":{"rendered":"<p>While working on my app <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.mohammedlakkadshaw.ken&#038;hl=en\">Android app Ken<\/a>, I needed to render HTML in Android, I was very much delighted when I found out that android\u2019s TextView supports HTML rendering and it can be easily achived by converting html into <a href=\"http:\/\/developer.android.com\/reference\/android\/text\/Spannable.html\">Spannable<\/a> using <strong>Html.fromHtml(<html_Content)<\/strong> method and sending it to TextView\u2019s setText method.<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>TextView myTxt = (TextView)findViewById(R.id.textView);\nmyTxt.setText(Html.fromHtml(htmlContent,ImageHandler,TagHandler));\n<\/code><\/pre>\n<\/div>\n<p>But there is catch with Html.fromHtml method, it doesn\u2019t 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.<\/p>\n<h2 id=\"handling-html\">Handling Html<\/h2>\n<p>Here is my custom tagHandler:<br \/>\n<script src=\"https:\/\/gist.github.com\/mlakkadshaw\/5983704.js\"><\/script><\/p>\n<p>The above Java file HtmlTagHandler handles, ul, ol, and code tags.<br \/>\nThe <strong>handleTagList()<\/strong> 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 <strong>Spannable.SPAN_MARK_MARK<\/strong> and when the tag on end I just find the object where I marked <strong>Spannable.SPAN_MARK_MARK<\/strong> using <strong>getLast()<\/strong> and find its position, and store it in the variable name <strong>\u201cwhere\u201d<\/strong>; that\u2019s 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 \u201cmonospace\u201d using <strong>(new TypefaceSpan(\u201cmonospace\u201d)<\/strong>.<\/p>\n<p>That\u2019s 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.<\/p>\n<p>Also try out my app <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.mohammedlakkadshaw.ken&#038;hl=en\">Ken which is available on Google Play<\/a> 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>\n<p>P.S: For handling image I using a nice library called <strong>\u201cURLImageParser\u201d<\/strong> <a href=\"https:\/\/gist.github.com\/Antarix\/4167655\">Github link<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s TextView supports HTML rendering and it can be easily achived by converting html into Spannable using Html.fromHtml(<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":0,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}