Android WebView Example - javatpoint [Link]
com/android-webview-example
Android WebView Example
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 [Link] 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 [Link] web page
using web view.
WebView mywebview = (WebView) findViewById([Link].webView1);
[Link]("[Link]
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([Link].webView1);
[Link]("[Link]
Let's see another code to display HTML code of a string.
String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>";
[Link](data, "text/html", "UTF-8");
Complete Android WebView Example
Let's see a complete example of Android WebView.
activity_main.xml
1 of 4 11/17/2019, 2:27 PM
Android WebView Example - javatpoint [Link]
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link] xmlns:android="[Link]
/apk/res/android"
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
WebView mywebview = (WebView) findViewById([Link]);
2 of 4 11/17/2019, 2:27 PM
Android WebView Example - javatpoint [Link]
// [Link]("[Link]
/*String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>";
[Link](data, "text/html", "UTF-8"); */
[Link]("[Link]
}
}
Output:
Let's see the output if you load the HTML page.
Let's see the output if you load the [Link] web page.
3 of 4 11/17/2019, 2:27 PM
Android WebView Example - javatpoint [Link]
4 of 4 11/17/2019, 2:27 PM