32

I'm trying to set the URL for a WebView from the layout main.xml.

By code, it's simple:

WebView webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/index.html");

Is there a simple way to put this logic into the layout XML file?

Pang
  • 9,564
  • 146
  • 81
  • 122
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
  • 5
    Notice the name _layout_ xml file. I don't think there is any way to do this from xml. I see your point, you want to be able to load it with data on the device without having to load the url from code, but I do not think there is any support for this now. – Eric Nordvik Mar 07 '11 at 12:53
  • Goal to Cant0na for seeing my point! – Vidar Vestnes Mar 07 '11 at 13:06

3 Answers3

6

You can declare your custom view and apply custom attributes as described here.

The result would look similar to this:

in your layout

<my.package.CustomWebView
        custom:url="@string/myurl"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

in your attr.xml

<resources>
    <declare-styleable name="Custom">
        <attr name="url" format="string" />
    </declare-styleable>
</resources>

finally in your custom web view class

    public class CustomWebView extends WebView {

        public CustomWebView(Context context, AttributeSet attributeSet) {
            super(context);

            TypedArray attributes = context.getTheme().obtainStyledAttributes(
                    attributeSet,
                    R.styleable.Custom,
                    0, 0);
            try {
                if (!attributes.hasValue(R.styleable.Custom_url)) {
                    throw new RuntimeException("attribute myurl is not defined");
                }

                String url = attributes.getString(R.styleable.Custom_url);
                this.loadUrl(url);
            } finally {
                attributes.recycle();
            }
        }
    }
loklok
  • 79
  • 1
  • 7
3

With Kotlin and binding adapter you can create a simple attribute for the Webview

Create file BindingUtils.kt

@BindingAdapter("webViewUrl") <- Attribute name
fun WebView.updateUrl(url: String?) {
    url?.let {
        loadUrl(url)
    }
}

and in the xml file: app:webViewUrl="@{@string/licence_url}"

        android:id="@+id/wvLicence"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        **app:webViewUrl="@{@string/licence_url}"**
        tools:context=".LicenceFragment"/>
Maor Hadad
  • 1,850
  • 21
  • 36
-5

Since URL is basically a string, you can put it into values/strings.xml file

<resources>
    <string name="myurl">http://something</string>
</resources>

then you can use it like this:

WebView webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(getString(R.string.myurl));
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • 36
    I think he is trying to find some way to set the url directly in the xml-file without having to run the method loadUrl in the Activity. Like: – Eric Nordvik Mar 07 '11 at 13:06
  • Thanks for the suggestion. This is surtainly a way to get the url moved out from the code and into a res file, by I was hoping I could the the url directly from the layout xml file, and not be forced to run the loadUrl(...) by code. – Vidar Vestnes Mar 07 '11 at 13:08
  • 23
    This should not be a selected answer. It does not answer this question. – Rob Mar 27 '14 at 21:37