Simple Android WebView code for accessing a website within app
Here in this post I am going give you simple android WebView code to open a website through an android app. The WebView class is a built-in extension of Android’s View class that allows you to display web pages as a part of your activity layout.
How to add WebView in android the layout?
To add a WebView to your Application, you need to include the
<WebView> element in your activity layout XML. Below is an example code:
<WebView android:id="@+id/my_webview" android:layout_width="match_parent" android:layout_height="match_parent" />
Then use loadUrl() to load a web page in your application:
WebView myWebView = (WebView) findViewById(R.id.my_webview); myWebView.loadUrl("https://www.codespeedy.com");
To load the web page in your web view your application must request the INTERNET permission from the manifest file.
That’s all you need to do for a basic WebView application which loads a web page in the app.
Now you will see that all the link from the app is opening via the default browser of your Android device. However, it needs to open in the app through WebView. To do this you need to add this code:
Android WebView code with basic features:
I have already created a complete WebView app with these features given below:
- Showing a progress bar while loading the web page first time after starting the app.
- Check internet connection if it is available and display no internet connection when there are no internet connection is available.
You can buy the complete source code from here only for $3 – https://gumroad.com/l/Android-Studio-WebView-Source-Code.
the source code is mainly for the newbies android developers. You can improve the source code and implement more features.