package com.example.testapp;

import java.util.ArrayList;
import java.util.Collections;

import android.content.Context;
import android.text.Html;
import android.widget.TextView;

/** This class helps create custom TextView with colours, emphasis tags, etc... */
public class DecoratedView {

	public class Tag {
		/** For example 'p' or 'h2' */
		public String name;
		/** If used, please prefix with single space */
		public String attributes;

		public Tag(String _name, String _attributes) {
			name = _name;
			attributes = _attributes;
		}
	}

	private String mHtml = "";

	public void appendText(String msg) {
		mHtml += msg;
	}

	public void appendText(String msg, int colour) {
		// TODO: ensure the hex string is 6 characters long
		Tag t = new Tag("font", " color=\"#" + Integer.toHexString(colour)
				+ "\"");
		ArrayList<Tag> tags = new ArrayList<Tag>();
		tags.add(t);
		appendTextInternal(msg, tags);
	}

	/** Example, names can be passed in as {"strong", "em"} */
	public void appendText(String msg, ArrayList<String> names) {
		ArrayList<Tag> tags = new ArrayList<Tag>();
		for (String s : names) {
			tags.add(new Tag(s, ""));
		}
		appendTextInternal(msg, tags);
	}

	public void appendText(String msg, int colour, ArrayList<String> names) {
		ArrayList<Tag> tags = new ArrayList<Tag>();
		// TODO: ensure the hex string is 6 characters long
		Tag t = new Tag("font", " color=\"#" + Integer.toHexString(colour)
				+ "\"");
		tags.add(t);
		for (String s : names) {
			tags.add(new Tag(s, ""));
		}
		appendTextInternal(msg, tags);
	}

	private void appendTextInternal(String msg, ArrayList<Tag> tags) {
		for (Tag t : tags) {
			mHtml += '<' + t.name + t.attributes + '>';
		}

		mHtml += msg;

		Collections.reverse(tags);
		for (Tag t : tags) {
			mHtml += "</" + t.name + '>';
		}
	}

	public String getHtmlString() {
		return mHtml;
	}

	public TextView getTextView(Context c) {
		TextView tv = new TextView(c);
		tv.setText(Html.fromHtml(mHtml));
		return tv;
	}

}

@@ -114,16 +114,47 @@
 ontext;%0A
+import android.graphics.Color;%0A
 import a
@@ -171,16 +171,16 @@
 t.Html;%0A
-
 import a
@@ -715,62 +715,8 @@
 ) %7B%0A
-%09%09// TODO: ensure the hex string is 6 characters long%0A
 %09%09Ta
@@ -752,36 +752,38 @@
 =%5C%22#%22 + 
-Integer.toHexString(
+String.format(%22%2506x%22, 
 colour)%0A
@@ -1284,62 +1284,8 @@
 ();%0A
-%09%09// TODO: ensure the hex string is 6 characters long%0A
 %09%09Ta
@@ -1325,28 +1325,30 @@
 %22 + 
-Integer.toHexString(
+String.format(%22%2506x%22, 
 colo
@@ -1910,10 +1910,526 @@
 tv;%0A%09%7D%0A%0A
+%09/**%0A%09 * This is an example of how to use this class, although this code may not%0A%09 * run in an Android environment.%0A%09 *%0A%09 * @param args%0A%09 *            unused%0A%09 */%0A%09public static void main(String%5B%5D args) %7B%0A%09%09DecoratedView dv = new DecoratedView();%0A%0A%09%09dv.appendText(%22hello world%22);%0A%09%09dv.appendText(%22the sky is blue%22, (Color.BLUE));%0A%09%09ArrayList%3CString%3E names = new ArrayList%3CString%3E();%0A%09%09names.add(%22strong%22);%0A%09%09dv.appendText(%22the sun is red and strong%22, Color.RED, names);%0A%0A%09%09System.out.println(dv.getHtmlString());%0A%09%7D%0A
 %7D%0A

Fix colour display bug (need to pad with 0's to make it 6 hex digits).