The concept here is webview. We look at various webview examples and libraries.
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Oclemy
Android WebView
Android WebView Tutorial and Examples.
This is an android WebView tutorial and Examples.
A WebView is a view that displays web pages.
What is a WebView?
A WebView, as we’ve said, is a WebView is a view that displays web pages.
It is represented by the
WebView
class. This class is much more powerful than you might think. Yet it is very easy and straighforward to use and provides you abstractions you build upon.It is this class that is the basis upon which you can create your own web browser or simply display some online content within your Activity. WebView makes use of the WebKit rendering engine to display web pages and includes methods t:
WebView is very powerful as it provides you with a way to write applications in languages like Javascript and the HTML markup. There are so many frameworks that make use of this capability, thus allowing you write your app in HTML5 technologies. You can even turn your website, like say a wordpress website into an android app.
WebView API Definition
WebView
is a concrete class residing in theandroid.webkit
package. It derives from theandroid.widget.AbsoluteLayout
class and implements several interfaces as shown below:Here’s it’s inheritance hierarchy:
Why Webview?
WebView is probably one of the most practical, easy to use yet underused classes in android. This is because it basically allows you to create an android app using HTML, CSS and Javascript. I would understand if it wasn’t used so much if it couldn’t execute Javascript or render CSS. However it does all those.
This then provides with powerful capabilities as HTML, CSS and Javascript are easy to use, popular technologies powering the user interfaces of almost every web app or website you have ever visited. Moreover there are hundreds of frameworks/libraries for these technologies that provide powerful widgets and abstractions. These include jQuery, Vue.js, Angular, React.js, Bootstrap, materializecss, UIKit etc.
You can easiliy create a simple client-side web app that can interact with server side technologies like Node.js and PHP then place in your assets folder. Then use WebView to load it. You have to ensure Javascript is enabled however. This, I understand is not as powerful as having a full Java application written in Java or Kotlin or C#, however, for beginners, you would quickly bootstrap your first app that you can show friends as you continue your education.
Using WebView
Most of the time you will want to render online content in your webview. So in order for your Activity to access the Internet and load web pages in a WebView, you must add the
INTERNET
permissions to your Android Manifest file:Then in your layout add a
<WebView>
in your layout, or set the entire Activity window as a WebView during onCreate():Once you’ve done that then you can load you webpage via the
loadUrl()
method:loadurl()
will load our website from the url we supply. This is the most commonly used way.You can also load from an HTML string:
This basically means you write your HTML code inside a string. Then load it via the
loadData()
method. This is suitable for loading websites with simple DOM(Document Object Model) structure.Quick WebView Examples and HowTo’s
Let’s look at quick howTos for our webview class. Then later on we will see how to write a full app.
1. Commonly used WebView settings
Here are some comminly used WebView settings. Let’s encapsulate them in a simple static method that we can then easily re-use.
That method is taking a
WebView
object. We first obtain the webview settings via thegetSettings
method of theWebView
class. Then we enable javascript via thesetJavaScriptEnabled()
method. Most of these settings methods take in a boolean value to either enable or disable various settings.2. How to Create a Custom WebView
We want to create a custom webview that can be used in a NestedScrollView.
1. Android WebView – Load From URL, Strings and Asset Folder
[center] Android WebView – Load From URL, Strings and Asset Folder [/center]
WebView is actually one of those classes that have existed in android since the beginning.
Added in API level 1, it resides in android.webkit package. It is used to display web content right within the activity. This makes it very powerful and can be used to build even a basic browser that works. It is still a view so we can simply drag it over from the pallete to our layout. It renders web pages using webkit rendering engine. In this example we use a webview to render web content from :
To load from url, you must android internet permission in the androidmanifest.xml. You can find more details about WebView here.
Screenshot
Android WebView Example
Common Questions this example explores
Tools Used
This example was written with the following tools:
No third party library was used in this project.
Lets jump directly to the source code.
AndroidManifest.xml
Build.Gradle
MainActivity.java
ActivityMain.xml
ContentMain.xml
menu_main.xml”
Download
Conclusion.
We saw a simple android webview example. How to load webpages from online via url, from assets folder and from string data.