add to the Webview content, link and javascript test

This commit is contained in:
Alexander Mahr 2024-10-09 09:25:39 +02:00
parent 07ab6c4ab1
commit bf535476ec

View file

@ -17,15 +17,23 @@ public class ExampleApp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// we create the webview (at least on the android 14 that is a webview that features avif + websockets etc....)
WebView myWebView = new WebView(this);//activityContext);
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
WebViewClient myWebViewClient= new WebViewClient();
myWebView.setWebViewClient(myWebViewClient);
String unencodedHtml = "<!DOCTYPE html><html><a href='https://html5test.co/'>https://html5test.co/</a><h1> this is html <h1> <h2> this is a h2</h2><img src='https://wald.alexmahr.de/images/rabe.avif'></html>";
String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(),Base64.NO_PADDING);
// to setup settings
WebSettings myWebSettings = myWebView.getSettings();
myWebSettings.setBuiltInZoomControls(true);
myWebSettings.setDisplayZoomControls(false);
myWebSettings.setJavaScriptEnabled(true);
// providing a webpage inline
String unencodedHtml = "<!DOCTYPE html><html><script> window.addEventListener('load',()=>{d=document.createElement('div');d.innerHTML='JAVASCRIPT works';document.body.appendChild(d);},false);</script><a href='https://html5test.co/'>https://html5test.co/</a><h1> this is html <h1> <h2> this is a h2</h2><img src='https://wald.alexmahr.de/images/rabe.avif'></html>";
String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(),Base64.NO_PADDING);
myWebView.loadData(encodedHtml, "text/html", "base64");
// alternatively this could be to load a website
//myWebView.loadUrl("https://alexmahr.de/ru");
setContentView(myWebView);
}