After a long break caused by some technical difficulties I have resumed working on my app. This time I added WebView there. It is a feature that allows users to open a webpage directly in their app.
First of all, my app had to request permission to get access to the Internet. It did so using the following line of the Manifest code:
<uses-permission android:name="android.permission.INTERNET" />
I would like the samd.board.ch webpage to open on the main screen of my app as soon as someone starts using it. To achieve this goal I added this to the MainActivity:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://board.samd.ch/");
I added JavaScript decoder so that my app could display the webpage.
A difficulty I faced was that every next page except the first one was opening on the Internet, but not in my app. That’s why I added a few lines of the code so it can check what webpage a “click” goes from. If it goes from the original page the app won’t redirect users to the Internet. So this is the checking function:
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://board.samd.ch/")) {
Toast.makeText(getApplicationContext(),
"Do not open the Browser",
Toast.LENGTH_SHORT).show();
return false;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
});
I decided to test my app using other webpages. Surprisingly, it didn’t work with the page my supervisor had created for me. The only difference between these two pages was https or http at the beginning. I tested some other webpages: it worked properly with the ones starting with https and it didn’t for those with http.
Questions to consider:
- What is the difference between https and http webpages?
- Why doesn’t my app work with http webpages?
- Which is better — to add a special button that can redirect the user to the webpage or to open it immediately in the app?