5

Is there a way to define a webview in the layout xml rather than in the code.

And if so how? Or is it recommended that it's coded in to an activity?

panthro
  • 22,779
  • 66
  • 183
  • 324

3 Answers3

10

Yes as above use the WebView tag:

<WebView android:id="@+id/webview"
android:layout_width="fill_parent" android:layout_height="50dip"/>

A sample application can be found here: http://www.androiddom.com/2011/04/creating-android-calculator-tutorial.html

The author creates a calculator that uses the WebView which is specified in the main.xml layout.

Lucian
  • 174
  • 10
0

Yes, use the <WebView /> tag to do this in the xml layout.

prolink007
  • 33,872
  • 24
  • 117
  • 185
  • 1
    Great, can I set the url by the xml too? – panthro Aug 02 '12 at 14:33
  • 1
    I do not believe you can. Here is another post with a similar question. http://stackoverflow.com/questions/5219794/how-to-set-url-on-webview-from-a-xml-layout-file-on-android – prolink007 Aug 02 '12 at 14:36
0

and in your code type this :

WebView wv = (WebView)findViewById(R.id.webview);
wv.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
wv.loadUrl("your url here");
amine
  • 31
  • 2