Search code examples
androidwebviewrssgoogle-glassgoogle-gdk

How to scroll webview in Google Glass


I have an Rss feed app displaying title and description from xml. I'm parsing the xml and displaying the content inside webview. When i navigate to webview, i can't able to scroll the content. It's like static. Is there is way to scroll content inside webview?

webview:

title = (TextView) findViewById(R.id.title);
        desc = (WebView) findViewById(R.id.desc);

        // set webview properties
        WebSettings ws = desc.getSettings();
        ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        ws.getPluginState();
        ws.setPluginState(PluginState.ON);
        ws.setJavaScriptEnabled(true);
        ws.setBuiltInZoomControls(true);

        // Set the views
        title.setText(feed.getItem(pos).getTitle());

        desc.loadDataWithBaseURL("http://www.googleglass.gs/", feed
                .getItem(pos).getDescription(), "text/html", "UTF-8", null);

Parsing:

public class DOMParser {

    private RSSFeed _feed = new RSSFeed();

    public RSSFeed parseXml(String xml) {

        URL url = null;
        try {
            url = new URL(xml);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        try {
            // Create required instances
            DocumentBuilderFactory dbf;
            dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            // Parse the xml
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            // Get all <item> tags.
            NodeList nl = doc.getElementsByTagName("item");
            int length = nl.getLength();

            for (int i = 0; i < length; i++) {
                Node currentNode = nl.item(i);
                RSSItem _item = new RSSItem();

                NodeList nchild = currentNode.getChildNodes();
                int clength = nchild.getLength();

                // Get the required elements from each Item
                for (int j = 1; j < clength; j = j + 2) {

                    Node thisNode = nchild.item(j);
                    String theString = null;
                    String nodeName = thisNode.getNodeName();

                    theString = nchild.item(j).getFirstChild().getNodeValue();

                    if (theString != null) {
                        if ("title".equals(nodeName)) {
                            // Node name is equals to 'title' so set the Node
                            // value to the Title in the RSSItem.
                            _item.setTitle(theString);
                        }

                        else if ("content:encoded".equals(nodeName)) {
                            _item.setDescription(theString);

                            // Parse the html description to get the image url
                            String html = theString;
                            org.jsoup.nodes.Document docHtml = Jsoup
                                    .parse(html);
                            Elements imgEle = docHtml.select("img");
                            _item.setImage(imgEle.attr("src"));
                        }

                        else if ("pubDate".equals(nodeName)) {

                            // We replace the plus and zero's in the date with
                            // empty string
                            String formatedDate = theString.replace(" +0000",
                                    "");
                            _item.setDate(formatedDate);
                        }

                    }
                }

                // add item to the list
                _feed.addItem(_item);
            }

        } catch (Exception e) {
        }

        // Return the final feed once all the Items are added to the RSSFeed
        // Object(_feed).
        return _feed;
    }

}

Solution

  • You have to implement your own WebView and implement that functionality or you can use Native Google Glass WebBrowser with an Intent.

    EDIT: You also can try to use native webBrowser with an intent:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    intent.setClassName("com.google.glass.browser", "com.google.glass.browser.WebBrowserActivity");
    intent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(intent);
    

    You create a HTML file with the content of your XML, you save it in the sdcard and then you open it with Intent.