Ads

Sunday, March 4, 2012

WebView in Appcelerator Titanium

The Titanium Web View allows you to open an HTML5 based view which can load either local or remote content. Here I am going to give example for both Local and remote content.

1. WebView with local content
Here I have added some sample text to webview, the 'html' property will accept the HTML content.

var win = Ti.UI.currentWindow;
var textContent = '
Hi this is the sample text from the webview
';
var localWebview = Titanium.UI.createWebView({
top:0,
left:10,
right:10,
html:textContent,
height:320,
width:320,
backgroundColor:'transparent',
touchEnabled:false
});
win.add(localWebview);

2. WebView with remote content
Here I have added some remote content into the webview, for example I am loading a Facebook page in webview.

var win = Ti.UI.currentWindow;
Ti.App.fireEvent('show_indicator'); //display loading spinner until webview gets loaded
var fbUrl = "http://www.facebook.com";
var extwebview = Titanium.UI.createWebView({
top:0,
left:0,
right:0,
url:fbUrl,
height:440,
width:320,
backgroundColor:'#ccc'
});
win.add(extwebview); //adding webview in current window

extwebview.addEventListener('load', function() {
Ti.App.fireEvent('hide_indicator'); //Hide the Loading indicator after the webview loaded
})

var space = Titanium.UI.createButton({
systemButton: Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});

var buttonClose = Titanium.UI.createButton({
title:"Done",
style:Titanium.UI.iPhone.SystemButtonStyle.DONE
});

var bottomToolbar = Titanium.UI.createToolbar({
left : 0,
bottom: 0,
height : 40,
width : 320,
items: [space,space, space,space, space, space,buttonClose]
})
win.add(bottomToolbar); // add close button at the bottom tabbar to close the webview

buttonClose.addEventListener('click', function (e) {
win.remove(extwebview);
win.remove(bottomToolbar);
});

5 comments:

  1. I have tried the code above and i get an undefined for the var space...section, does it work for you?

    ReplyDelete
    Replies
    1. Hi Ilan, Thanks for reading my blog.

      Have you declared

      var space = Titanium.UI.createButton({
      systemButton: Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
      });

      also the above property is iPhone specific one. It doesn't work in Android.

      Delete
  2. Hey,


    Try with a clean project if this problem still persists.

    ReplyDelete
  3. Where do I put the code? In what file too?

    ReplyDelete
  4. hi, The code which you posted is right. But I want play youtube videos using video player or webview. I tried in many ways but didnt get the proper solution. Can you share any sample that works fine playing youtube videos.

    ReplyDelete