Kotlin Android – WebView – Example
Kotlin Android – WebView
Android AlertDialog class is used to display a web page embedded in the Android Activity. The web page could
be displayed in the same Android Application, without opening a browser window.
WebView could be used in your Android Application like any other View (say TextView, Button, etc.).
To display WebView in your Android Activity
1. Add permission for INTERNET in AndroidManifest.xml. Applications that make use of WebView require INTERNET
permission. To let Android know that our application require INTERNET, include the following line in
AndroidManifest.xml. <uses-permission android:name="android.permission.INTERNET" />
2. Include Android WebView In layout fileYou can include following WebView snippet in Layout file to display WebView in
Activity.
WebView in Layout File
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3. Load URL in WebView programmatically in Activity file. webView.loadUrl("https://www.google.com/")
Example – Android AlertDialog
In this Example Kotlin Android Application, we shall display a WebView and load “www.google.com url”.
AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorialkart.webviewexample">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEE"
tools:context=".MainActivity">
<TextView
android:text="Web View Example"
android:gravity="center"
android:padding="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.kt
package com.tutorialkart.webviewexample
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView.loadUrl("https://www.google.com/")
}
}
Download Complete Source Code of this Example Android Application
Conclusion
In this Kotlin Android Tutorial – AlertDialog Example, we have learnt to display an Alert Dialog box in Activity.
We also learnt to set title, message and Positive, Negative buttons of AlertDialog.
Getting Started with Android
Kotlin Android Tutorial
Create Android Application with Kotlin Support
Create Android Application with Kotlin Support
Walk Through Android Studio
Convert Java Files to Kotlin Files
Kotlin vs Java
Use Java 8 in Android
Add External Jar to Android Dependencies
Android TextView
Android TextView - Basic Example
Android TextView - Create programmatically
Android TextView - OnClickListener
Android TextView - Justify Text
Android Button
Android - New Button programmatically
Android Button - OnClickListener
Android Button - Disable All Caps
Android Button - Custom Background
Android Button - Change background programatically
Android Toast
Android Toast - Example
Android EditText
Android EditText - Create programmatically
Android EditText - On Text Change - Listener
Android TextInputLayout - Floating Label in EditText
Android EditText - Keyboard with only Numbers
Android EditText - Show/Hide Password
Android ImageView
Android ImageView - OnClickListener
Android Radio Buttons
Android RadioGroup - RadioButtons Create programmatically
Android SeekBar
Android SeekBar - Example
Android SeekBar Set Custom Range
Android Intent
Android - Start Another Activity
Android - Open URL in Browser Activity
Android AlertDialog
Android AlertDialog - Example
Android WebView
Android WebView - Example
Android ProgressBar
Kotlin Android - Inderterminate ProgressBar
Android Snackbar
Android Snackbar - Example
Android Snackbar - Set Action
Android Snackbar - Change Text Color, Background Color
Android ListView
Android ListView Example
Android Refresh ListView
Android Device Parameters
Android Get Screen Width and Height Programatically
Android Canvas
Draw Rect / Oval to Canvas
Android Draw Circle Border
Android Draw SVG to Canvas
Android Programming - Other
Android - Access View Programmatically using findViewById
Android runOnUiThread
Android Game Development
Android Game Development
Detect Collisions between two Sprites (Bitmaps)
Android Text To Speech
Android Text To Speech - Kotlin Example
Fix Errors
Android - Minimum supported Gradle version
Android - All support libraries must use the exact same version specification
Example Applications
Android - Login Form
Android - Color Picker
Kotlin Android Game Development
Kotlin - Java
Kotlin Tutorial
Useful Resources
How to Learn Programming