12-20-2010 03:51 AM
hi all,
i would like to display html content(similar to the webpage rendering) on the BlackBerry screen. Content is store in string variable. So, is there any way to display this html data on BB screen?
it working with os 5.0 becoz browserfield is avialable in 5.0 but i wana to implement in os 4.5
any one have any guess its urgent
thanx in advance
Kiran kumar choudhary
12-20-2010 04:40 AM
I believe this question has been answered a number of times on the forum. Have you searched for a solution?
12-20-2010 04:50 AM
Hi peter
thahnx for reply
yeah ur right I found some code but not getting any sucess if u have any code than plz send me
12-20-2010 04:56 AM
12-20-2010 05:06 AM
Hi this code is given by peter but i dont know why is not working for me
import java.util.Stack;
import java.util.Vector;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.component.RichTextField;
/** Class that implements very basic formatting of a label
* field using HTML codes. Only the basic XHTML elements
* are currently supported:
* <ul>
* <li>tt: Renders as teletype or monospaced text.</li>
* <li>i: Renders as italic text style.</li>
* <li>b: Renders as bold text style.</li>
* <li>big: Renders text in a "large" font.(1.2 times bigger, as specified in CSS-2)</li>
* <li>small: Renders text in a "small" font. (1.2 times smaller, as specified in CSS-2</li>
* </ul>
*
* This field does not support data that contains those
* embedded items in the string, since they will be considered
* as xhtml codes.
*
* Validation is strictly limited.
*
* @author Carl Eric Codere
*
*/
public class HTMLTextField extends RichTextField
{
private static final String[] TAGS = {"i", "b","tt","big","small"};
//Type
private static final int TAG_STYLE_ITALICS = 0x01;
private static final int TAG_STYLE_BOLD = 0x02;
private static final int TAG_STYLE_TELETYPE = 0x04;
private static final int TAG_STYLE_NORMAL = 0x00;
//Size
//These two last items are exclusive.
private static final int TAG_SIZE_NORMAL = 0x80;
private static final int TAG_SIZE_BIG = 0x82;
private static final int TAG_SIZE_SMALL = 0x84;
private static final int[] TAG_TYPES =
{TAG_STYLE_ITALICS,TAG_STYLE_BOLD,TAG_STYLE_TELETY
public HTMLTextField(String text, int[] offsets, byte[] attributes, Font[] fonts, long style)
{
super(text, offsets, attributes, fonts, style);
}
private static final class ElementState
{
int startPos;
int endPos;
int tagType;
public ElementState(int currentPos)
{
startPos = currentPos;
tagType = TAG_STYLE_NORMAL | TAG_SIZE_NORMAL;
}
}
private static class ElementStateList extends Vector
{
public void add(ElementState o)
{
ElementState e;
int i;
// We merge this element with another element of this type
// if it already exists at this offset.
for (i= 0; i < size(); i++)
{
e = (ElementState)elementAt(i);
// There is a match, then we must merge the data
if (e.startPos == o.startPos)
{
e.tagType = e.tagType | o.tagType;
e.endPos = o.endPos;
return;
}
}
super.addElement(o);
}
}
private static int getTagType(String s)
{
int i;
for (i = 0; i < TAGS.length; i++)
{
if (s.equals(TAGS[i]))
{
return (TAG_SIZE_NORMAL | TAG_TYPES[i]);
}
}
return -1;
}
/** This method parses a text string containing XHTML markup text which is limited
* to only certain elements, and then creates a correct RichTextField field
* already setup correctly with all correct handling of the markup..
*
* @param text The text, optionally containing valid XHTML markup
* @param style The style
* @return a new instance of HTMLTextField
* @throws java.lang.IllegalArgumentException
*/
public static HTMLTextField getInstance(String text, long style) throws IllegalArgumentException
{
int i = 0;
char c;
int fontStyle;
ElementState currentState;
ElementState e;
Stack stack;
String currentTag ="";
String cleanString = "";
boolean validTag = false;
int tagType;
ElementStateList states = new ElementStateList();
stack = new Stack();
currentState = new ElementState(0);
// Push the normal state element
states.add(currentState);
i = 0;
while (i < text.length())
{
c = text.charAt(i);
if (c == '<')
{
currentTag = "";
i++;
validTag = false;
while (i < text.length())
{
c = text.charAt(i);
if (c == '>')
{
validTag = true;
break;
}
currentTag = currentTag + c;
i++;
}
// The end of the text string was reached without an end
// tag.
if (validTag == false)
{
cleanString = cleanString + "<"+ currentTag;
i++;
continue;
}
// Now check if this is an end tag or not
if (currentTag.charAt(0) == '/')
{
currentTag = currentTag.substring(1);
tagType = getTagType(currentTag);
if (tagType == -1)
{
cleanString = cleanString + "</"+currentTag+">";
i++;
continue;
}
// Illegal value
if (stack.empty()==true)
throw new IllegalArgumentException("Mssing start tag for "+currentTag);
e = (ElementState)stack.peek();
// The values are bitmasks
if ((e.tagType & tagType)==0)
{
throw new IllegalArgumentException("Missing start tag for "+currentTag);
}
stack.pop();
e.endPos = cleanString.length();
states.add(e);
// Push the normal state element
currentState = new ElementState(cleanString.length());
states.add(currentState);
} else
{
// This is the start of the element
tagType = getTagType(currentTag);
if (tagType == -1)
{
cleanString = cleanString+"<"+currentTag+">";
i++;
continue;
}
e = new ElementState(cleanString.length());
currentState.endPos = cleanString.length();
e.tagType = tagType;
stack.push(e);
}
}
else
cleanString = cleanString + c;
i++;
}
currentState.endPos = cleanString.length();
// Remove this last element if it points to invalid data.
if (currentState.endPos == currentState.startPos)
{
int deleteIdx = states.indexOf(currentState);
states.removeElementAt(deleteIdx);
}
// Normally all elements should have been emptied
if (stack.empty() == false)
{
throw new IllegalArgumentException("Missing end tag(s)");
}
// Now create the tables for the RichTextField...
int[] off = new int[states.size()+1];
byte[] attr = new byte[states.size()];
Font defaultFont = Font.getDefault();
Font[] fon = new Font[states.size()];
for (i = 0; i < states.size(); i++)
{
e = (ElementState)states.elementAt(i);
off[i] = e.startPos;
// Now retrieve the information on the changes in styles we have to do
fontStyle = Font.PLAIN;
if ((e.tagType & TAG_STYLE_ITALICS)==TAG_STYLE_ITALICS)
{
fontStyle |= Font.ITALIC;
}
if ((e.tagType & TAG_STYLE_BOLD)==TAG_STYLE_BOLD)
{
fontStyle |= Font.BOLD;
}
attr[i] = (byte)i;
//Ui.UNITS_pt
fon[i] = defaultFont.derive(fontStyle);
}
off[states.size()] = cleanString.length();
return new HTMLTextField(cleanString,off,attr,fon,style);
}
}
12-20-2010 05:12 AM
12-20-2010 05:17 AM
thanx for reply but not give any exception but it not render my html not give any effect
12-20-2010 05:52 AM
hi all
my code is not working where we need anchor tag like <a href=""> so how to implement this plz let me know its very much urgent for me
thanx in advance
12-20-2010 05:54 AM
12-20-2010 05:57 AM