TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Android WebView Example

Android WebView Example with examples of Activity and Intent, Fragments, Menu, Service, alarm manager, storage, sqlite, xml, json, multimedia, speech, web service, telephony, animation and graphics

<< Back to ANDROID

Android WebView Example

android web view

Android WebView is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity.

Android WebView uses webkit engine to display web page.

The android.webkit.WebView is the subclass of AbsoluteLayout class.

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.



Android WebView Example

Let's see the simple code to display TheDeveloperBlog.com web page using web view.


WebView mywebview = (WebView) findViewById(R.id.webView1);
mywebview.loadUrl("http://www.TheDeveloperBlog.com/");

Let's see the simple code to display HTML web page using web view. In this case, html file must be located inside the asset directory.

WebView mywebview = (WebView) findViewById(R.id.webView1);
mywebview.loadUrl("file:///android_asset/myresource.html");

Let's see another code to display HTML code of a string.

String data = "<html><body><h1>Hello, TheDeveloperBlog!</h1></body></html>";
mywebview.loadData(data, "text/html", "UTF-8");

Complete Android WebView Example

Let's see a complete example of Android WebView.

activity_main.xml

File: activity_main.xml



    


To add the web page (.html, .jsp) locally in application, they are required to place in the assets folder. An assets folder is created as: right click on app -> New -> Folder -> Assets Folder ->main or simply create an assets directory inside main directory.

Activity class

File: MainActivity.java
package example.TheDeveloperBlog.com.webview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView mywebview = (WebView) findViewById(R.id.webView);
        // mywebview.loadUrl("https://www.TheDeveloperBlog.com/");

        /*String data = "

Hello, TheDeveloperBlog!

"; mywebview.loadData(data, "text/html", "UTF-8"); */ mywebview.loadUrl("file:///android_asset/myresource.html"); } }

Output:

Let's see the output if you load the HTML page.

android webview example output 1

Let's see the output if you load the TheDeveloperBlog.com web page.

android webview example output 2





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf