Haysky Technologies

Nandikotkur, Kurnool Dist.

+91 8498000172

Customer Support

Mon - Sat: 9:00 - 5:00

Office Timings

Webview in Android

Add this in activity_main.xml file

<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" />

In your MainActivity.kt file add this below setContentView(R.layout.activity_main)

var mywebview = findViewById<WebView>(R.id.webView)
mywebview!!.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url.toString())
return true
}
}
mywebview!!.loadUrl("https://haysky.com/")

// this will enable the javascript settings
mywebview.settings.javaScriptEnabled = true

// if you want to enable zoom feature
mywebview.settings.setSupportZoom(true)

Add this before the last flower bracket }

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {

var mywebview = findViewById<WebView>(R.id.webView)
if (event.getAction() === KeyEvent.ACTION_DOWN) {
when (keyCode) {
KeyEvent.KEYCODE_BACK -> {
if (mywebview.canGoBack()) {
mywebview.goBack()
} else {
finish()
}
return true
}
}
}
return super.onKeyDown(keyCode, event)
}

Finally add internet permission in Manifest.xml above the application tag

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Now you can run your APP.

Picture of Sufyan Shaik

Sufyan Shaik

Leave a Replay

Sign up for our Newsletter

Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit