I am a beginner in Android app development and Java. I want my app to use a WebView to display its content. The user should be able to interact with HTML elements, such as buttons or links, which will then send requests to my Android Java class to navigate to different pages.
To achieve this, my main class loads the WebView using the following code:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/html/status01.html");
Inside the public class JavaScriptInterface
, I would like to create a function that loads another URL:
public void showOffers() {
WebView myWebView = (WebView) ((Activity) mContext).findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/html/offers.html");
}
However, I encounter an error stating:
Activity cannot be resolved to a type
How can I access the WebView from my JavaScriptInterface class in order to load another URL?