The Webview API consists of the following classes and interfaces (packaged as a part of the Nokia API):
BrowserAdapter is a default implementation of the BrowserListener interface. MIDlets can use it as a convenience class to override the methods of the BrowserListener interface.
Use the BrowserItem class to render an html page defined by a URL.
Use the BrowserListener interface to handle callbacks from the BrowserItem.
For an example MIDlet that demonstrates how to use Webview API, see Webview API – BrowserItemDemo
The getBrowserItem() method of the BrowserItem class returns the BrowserItem, which can be added on Canvas or CustomItem. It is possible to create only one BrowserItem for one MIDlet.
BrowserItem browserItem = BrowserItem.getBrowserItem();
The following sample code shows, how to set BrowserListener, BrowserItem parent, its position and size. Finally the BrowserItem is set to visible:
browserItem.setBrowserListener(this); browserItem.setParent(canvas); browserItem.setPosition(40, 20); browserItem.setSize(150, 140); browserItem.setVisible(true);
The following code demonstrates how to render a web page by using invokeUrl(String url) method of the BrowserItem class:
try { browserItem.invokeUrl(url); } catch (IOException ioe) { } catch (SecurityException se) { } catch (IllegalArgumentException iae) { }
The BrowserListener interface can be used for handling the callbacks coming from the BrowserItem. The following methods must be created, when the interface is implemented:
void onConfirmDialog(java.lang.String message, java.lang.String leftText, java.lang.String rightText) {} void onFileSelectDialog() {} void onHttpStatusCode(int statusCode) {} void onItemSelected(java.lang.String name, int dataSize, java.lang.String mimeType) {} void onNoteDialog(java.lang.String message) {} void onPageChanged() {} void onPageInteraction() {} void onProgressChanged(int progress) {} void onProgressEnd() {} void onProgressStart() {} void onSecurityChanged(boolean securePage) {} void onSelectListDialog(java.lang.String title, boolean multiSelect, int[] idList, boolean[] selectedList, java.lang.String[] textList) {} void onUploadEnd() {} void onUploadStart() {}
Once the handling is complete for the onNoteDialog(), onConfirmDialog(), onFileSelectDialog() and onSelectListDialog() functions, ensure that you call the respective XXXDone() functions:
noteDialogDone(); confirmDialogDone(); fileSelectDialogDone(); selectListDialogDone();
If there is a list selection or a drop-down list on the web page, the MIDlet must implement the onSelectListDialog (String title, boolean multiSelect, int[] idList, boolean[] selectedList, String[] textList) BrowserListener interface method to show the list to the user and make it available for selection.
public void onSelectListDialog(String title, boolean multiSelect, int[] idList, boolean[] selectedList, String[] textList) { for (int i = 0; i < textList.length; i++) { System.out.println("#" + idList[i] + ": " + textList[i] + ": " + selectedList[i]); } selectionList = new SelectionList(title, List.IMPLICIT, textList, null, midlet); Display.getDisplay(midlet).setCurrent(selectionList); }
In SelectionList class (extending the javax.microedition.lcdui.List class) selecting the item and closing the List is handled as given below:
public void commandAction(Command c, Displayable d) { if (c == List.SELECT_COMMAND) { selectedIds[0] = this.getSelectedIndex(); midlet.browserCanvas.closeList(selectedIds); } }
Once the selection is made, the List is closed and theBrowserItem.selectListDialogDone(int[]ids) method must be called.
BrowserItem.selectListDialogDone(int[] ids) method should be called. protected void closeList(int[] ids) { Display.getDisplay(midlet).setCurrent(this); try { browserItem.selectListDialogDone(ids); } catch (IllegalStateException ise) { } catch (IllegalArgumentException iae) { } selectionList = null; }
In certain cases it is possible to select multiple items from the list. To do so, use list of type MULTIPLE and call the BrowserItem.selectListDialogDone(int[] ids) method, even if no selection is made.
If there is a file selection text field on the web page, the MIDlet must implement the onFileSelectDialog() BrowserListener interface method to show the actual file selection dialog. This can be done easily by using Nokia UI API’s com.nokia.mid.ui.FileSelect class, as given in the example below:
public void onFileSelectDialog() { Thread fileSelectThread = new Thread() { public void run() { try { FileSelectDetail arrSelectedFiles[] = FileSelect.launch(PHOTOS_FOLDER, type, false); if (arrSelectedFiles == null) { // If user goes back without selecting a file. browserItem.fileSelectDialogDone(null); return; } if (arrSelectedFiles.length > 0) { fileUrl = arrSelectedFiles[0].url; browserItem.fileSelectDialogDone(fileUrl); } } catch (IOException ioe) { midlet.showError("IOException", ioe.getMessage()); } catch (IllegalArgumentException iae) { midlet.showError("IllegalArgumentException", "Invalid file URL or no file selected."); } } }; fileSelectThread.start(); }
In the current implementation if the user cancels the file selection, the browserItem.fileSelectDialogDone(null); or browserItem.fileSelectDialogDone(“”); method must be called, however currently it throws an IllegalArgumentException error. This is a known issue.
Once the selection is made and the File Select Dialog is closed, the MIDlet must call the BrowserItem.fileSelectDialogDone(String path) method and give the path of the selected file as the parameter.