Blackberry BrowserField white empty page issue - blackberry

Hi i want to show html content in BrowserField. I used the code blove to do this but i only see white empty page.
BrowserField demo = new BrowserField();
String res="<html><body><p>demo</p></body></html>";
demo.displayContent(res, "http://localhost");
Sometimes it shows my page correctly with css fonts but sometimes it does not show anything.
What is the problem in my code?

If you look at this BlackBerry example,
they make sure to add() the BrowserField to its parent Manager before calling displayContent().
I don't know if you just omitted that line of code to shorten your question, if you're missing the call to add() entirely, or if you put it after the call to displayContent(). But, try doing it in the order listed in the BlackBerry example, and let me know if that works.
BrowserField demo = new BrowserField();
add(demo);
String res="<html><body><p>demo</p></body></html>";
demo.displayContent(res, "http://localhost");

Related

Adding components to BorderLayout

I'm trying to implement BorderLayout within IntelliJ and am having trouble getting it to work. It compiles fine, but when it runs I see the viewer for a second and then it crashes. The code I currently have is
Button Next=new Button("Next");
Button Back=new Button("Back");
Container panel1=new Container();
Container panel2=new Container();
home = new Form("Home");
home.setLayout(new BorderLayout());
panel1.setLayout(new BorderLayout());
panel2.setLayout(new BorderLayout());
home.addComponent(BorderLayout.EAST,panel1);
home.addComponent(BorderLayout.WEST,panel2);
panel1.addComponent(Next);
panel2.addComponent(Back);
The error I get after it crashes is "Cannot add component to BorderLayout Container without constraint parameter". I tried researching a constraint parameter and also working with BorderLayout in IntelliJ, but any texts I found were either not helpful or too complicated to understand. Thanks so much!
The error message is telling you that if you want to add a component to a border layout you have to specify where to put it. You've done this when adding components to home, but you haven't when you add components to panel1 and panel2. You need to add BorderLayout.EAST (or WEST, or whatever) to get:
panel1.addComponent(BorderLayout.EAST, Next);
panel2.addComponent(BorderLayout.WEST, Back);
However, I think you're using this wrong. You probably don't want a border layout in panel1 or panel2 -- they're fine with the default flow layout, so if you remove the panel1/2.setLayout() lines your code should work fine.
BTW: In java we don't use capital letters at the start of variables, so Next and Back should be next and back. Also, panel1 should be something like nextPanel, and panel2 should be something like backPanel.
To add one more thing -- nobody uses awt any more. We've all moved on (mostly to html5). So, trying this in swing, you would get:
import javax.swing.*;
...
JButton next = new JButton("Next");
JButton back = new JButton("Back");
JFrame home = new JFrame("Home");
home.setLayout(new BorderLayout());
home.add(back, BorderLayout.WEST);
home.add(next, BorderLayout.EAST);

Text URL in AIR iOS app not selectable

I'm using AIR 2.0 (soon will be updating to 3.3 with Flash CS6) to create an iPad app. We have textfields (Classic, dynamic) which sometimes contain one or multiple htmlText links which need to be clickable. In the desktop version of the program, all text is selectable and the links are easily accessed. My problem is that it takes me mashing the link like 20 times on the iPad before it will recognize that there's a link and navigate to it in Safari. The other strange thing is that none of the text appears to be selectable - I can't get the iPad cursor, copy/paste menu, etc. to show up.
I think, from reading other threads, that the hit area for the URL is only the stroke on the text itself... if that's true, what can I do to increase the hit area? Or make text selectable? It was suggested elsewhere to put movieclips behind the URLs but that's not really possible as this is all dynamic text from XML files.
I've read about StageText but I gather this is only used for input fields, which is not the case here.
I'm reasonably advanced in AS3 but I'd prefer an easy solution over re-writing large chunks of code. At the moment the only thing I can think to do is get the URL and make it so that as soon as you touch anywhere on the textfield, it navigates to the link. But this would break down if there were more than 1 URL in a given textfield.
Any ideas?
I had this exact same issue, and it's had me flummoxed for a while.
Here's what I did to get the desired behaviour:
1) Instead of using a listener for TextEvent.LINK, listen for MouseEvent.CLICK (or TouchEvent.TAP) on the TextField.
eg.
var tf:TextField = new TextField();
tf.addEventListener(MouseEvent.CLICK, linkClicked);
2) In the linkClicked() handler, you use getCharIndexAtPoint() to determine the index of the character that was clicked, and then from that determine the URL from the TextFormat of the character. This is adapted from a post by Colin Holgate on the Adobe Forums (http://forums.adobe.com/thread/231754)
public function linkClicked(e:MouseEvent):void {
var idx:int = e.target.getCharIndexAtPoint(e.localX, e.localY);
trace("Tapped:",idx);
var tf:TextFormat = e.target.getTextFormat(idx);
if(tf.url != "" && tf.url != null) {
var linkURL:String = tf.url;
trace(linkURL);
// Hyperlink processing code here
dispatchEvent(new UIEvent(UIEvent.LINK_TAPPED,tf.url));
}
}
3) The last line (dispatchEvent()) is sending a custom event to another function to process the link, but you could easily inline your code here.
I've tested on an iPad 3 running iOS6.1, building with AIR3.5. Links are much more responsive, and I don't find myself mashing the screen trying to hit the stroke of the text!

Blackberry BrowserField Base Url

I have been trying to show some HTML content from SD card in the browser Field and every time
instead of rendering the HTML data,browser Field renders the Base URL.I tried to find the reason for the same and couldn't get any solution.I didn't find any documentation that clearly explains when that base URL is called.Can any one please explain why and when is that Base URL called? Any Help is highly appreciated...
Thanks in advance.
May be it will help you. This answer explain how to display html content from sdcard in two ways
1)Read data as text file and add it to String and dislay that content like following way
StringBuffer sb1;
FileConnection fconn = (FileConnection)Connector.open("filepath="file:///SDCard/BlackBerry/documents/HtmlFileName.txt", Connector.READ_WRITE);
if (fconn.exists())
{
InputStream input=fconn.openDataInputStream();
sb1=new StringBuffer();
int chars,i=0;
while((chars=input.read())!=-1)
{
sb1.append((char)chars);
}
}
String str=sb1.toString();
VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLL_MASK);
BrowserFieldConfig browserFieldConfig = new BrowserFieldConfig();
browserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_POINTER);
browserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_CARET);
browserFieldConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,Boolean.TRUE);
browserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE_POINTER.toString(),Boolean.TRUE);
browserFieldConfig.setProperty(BrowserFieldConfig.ALLOW_CS_XHR,Boolean.TRUE);
BrowserField browser_field=new BrowserField(browserFieldConfig);
browser_field.displayContent(str,"");
vfm.add(browser_field);
add(vfm);
2)You can directly call that page from browserfield like following
String url="file:///sdcard/index.html";//specify exact path if resource then local:///.if sd card file:///
VerticalFieldManager manager=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR|HORIZONTAL_SCROLL|HORIZONTAL_SCROLLBAR){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(640,Display.getHeight());
setExtent(640,Display.getHeight());
}
};
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
BrowserField browserField = new BrowserField(myBrowserFieldConfig);
history=browserField.getHistory();
browserField.requestContent(url);
manager.add(browserField);
browserField.requestContent(url);
add(manager);
I realize this is a little old, but I had the same problem and wanted to share how I solved my case.
It turns out that if you try to navigate to a new page in the BrowserField and the new page can't be loaded or an error occurs, the BrowserField will load the Base URL.
This issue occurred twice to me. In one instance, my program threw a runtime exception which was handled silently (by accident). This caused the navigation to fail and the Base URL to load. In the second instance, the page I was attempting to navigate to did not exist (either by misspelling or misuse of file names or locations).
Just to be clear, it was the Base URL of the original page which was loaded, not the page that failed to load.
I hope this helps!

Editing a BrowserField's History

I have a BrowserField in my app, which works great. It intercept NavigationRequests to links on my website which go to external sites, and brings up a new windows to display those in the regular Browser, which also works great.
The problem I have is that if a user clicks a link to say "www.google.com", my app opens that up in a new browser, but also logs it into the BrowserHistory. So if they click back, away from google, they arrive back at my app, but then if they hit back again, the BrowserHistory would land them on the same page they were on (Because going back from Google doesn't move back in the history) I've tried to find a way to edit the BrowserField's BrowserHistory, but this doesn't seem possible. Short of creating my own class for logging the browsing history, is there anything I can do?
If I didn't do a good job explaining the problem, don't hesitate for clarification.
Thanks
One possible solution to this problem would be to keep track of the last inner URL visited before the current NavigationRequest URL. You could then check to see whether the link clicked is an outside link, as you already do, and if it is call this method:
updateHistory(String url, boolean isRedirect)
with the last URL before the outside link. Using your example this should overwrite "www.google.com" with the last inner URL before the outside link was clicked.
Here is some half pseudocode/half Java to illustrate my solution:
BrowserFieldHistory history = browserField.getHistory():
String lastInnerURL = "";
if navigationRequest is an outside link {
history.updateHistory(lastInnerURL, true);
// Handle loading of outer website
} else {
lastInnerURL = navigationRequest;
// Visit inner navigation request as normal
}
http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/browser/field2/BrowserFieldHistory.html#updateHistory(java.lang.String, boolean)
I had a similar but a little bit different issue. Special links in html content like device:smth are used to open barcode scanner, logout etc and I wanted them not to be saved in BrowserFieldHistory. I found in WebWork source code interesting workaround for that. All that you need is throw exception at the end like below:
public void handleNavigationRequest( BrowserFieldRequest request ) throws Exception {
if scheme equals to device {
// perform logout, open barcode scanner, etc
throw new Exception(); // this exception prevent saving history
} else {
// standard behavior
}
}

WatiN: Print Dialog

I have a screen that pops up on load with a print dialog using javascript.
I've just started using WatiN to test my application. This screen is the last step of the test.
What happens is sometimes WatiN closes IE before the dialog appears, sometimes it doesn't and the window hangs around. I have ie.Close() in the test TearDown but it still gets left open if the print dialog is showing.
What I'm trying to avoid is having the orphaned IE window. I want it to close all the time.
I looked up DialogHandlers and wrote this:
var printDialogHandler = new PrintDialogHandler(PrintDialogHandler.ButtonsEnum.Cancel);
ie.DialogWatcher.Add(printDialogHandler);
And placed it before the button click that links to the page, but nothing changed.
The examples I saw had code that would do something like:
someDialogHandler.WaitUntilExists() // I might have this function name wrong...
But PrintDialogHandler has no much member.
I initially wasn't trying to test that this dialog comes up (just that the page loads and checking some values on the page) but I guess it would be more complete to wait and test for the existence of the print dialog.
Not exactly sure about your situation, but we had a problem with a popup window that also displayed a print dialog box when loaded. Our main problem was that we forgot to create a new IE instance and attach it to the popup. Here is the working code:
btnCoverSheetPrint.Click(); //Clicking this button will open a new window and a print dialog
IE iePopup = IE.AttachToIE(Find.ByUrl(new Regex(".+_CoverPage.aspx"))); //Match url ending in "_CoverPage.aspx"
WatiN.Core.DialogHandlers.PrintDialogHandler pdhPopup = new WatiN.Core.DialogHandlers.PrintDialogHandler(WatiN.Core.DialogHandlers.PrintDialogHandler.ButtonsEnum.Cancel);
using (new WatiN.Core.DialogHandlers.UseDialogOnce(iePopup.DialogWatcher, pdhPopup)) //This will use the DialogHandler once and then remove it from the DialogWatcher
{
//At this point the popup window will be open, and the print dialog will be canceled
//Use the iePopup object to manage the new window in here.
}
iePopup.Close(); // Close the popup once we are done.
This worked for me:
private void Print_N_Email(Browser ie)
{
//Print and handle dialog.
ie.Div(Find.ById("ContentMenuLeft")).Link(Find.ByText(new Regex("Print.*"))).Click();//orig
Browser ie2 = Browser.AttachTo(typeof(IE), Find.ByUrl(new Regex(".*Print.*")));
System.Threading.Thread.Sleep(1000);
PrintDialogHandler pdh = new PrintDialogHandler(PrintDialogHandler.ButtonsEnum.Cancel);
new UseDialogOnce(ie2.DialogWatcher, pdh);
ie2.Close();
}
You still might want to check your browser AutoClose property ie.AutoClose

Resources