Skip to content Skip to sidebar Skip to footer

Android App: How To Loadurl In Webview From Another Class?

I´m quite new to Android app programming and Java. I want my App to use a WebView for showing all what is needed. The user can click on an HTML-Button or Link an this sends a requ

Solution 1:

Define the class JavaScriptInterface as inner class of your Activity and store the reference to the WebView as member variable of your Activity.

Because inner classes can access member variables of the class they are defined in, you can change your code to this:

MyWebActivityextendsActivity{
    privateWebView myWebView;
    protectedvoidonCreate(Bundle bundle){
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.addJavascriptInterface(newJavaScriptInterface(), "Android");
        myWebView.setWebViewClient(newWebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("file:///android_asset/html/status01.html");
    }

    privateclassJavaScriptInterface{
        JavaScriptInterface(){
        }
        publicvoidshowOffers() {                
            myWebView.loadUrl("file:///android_asset/html/offers.html");
        }
    }
}

Post a Comment for "Android App: How To Loadurl In Webview From Another Class?"