Convert Website into Android Application using Kotlin
Get Into Code previously provides you with free C++ and Java Course in Hindi/Urdu and also provides you the opportunity to practice your skills to be perfect. Now, Get Into Code provides you with android development tutorials as well using Kotlin Language, here is the first post which covers How to convert website into android application using kotlin.
- Get Started by adding Internet Permission in AndroidManifest.xml file in your android studio.
- Now, add the WebView to your activity_main.xml file in your android studioAdd the following code to your activity_main.xml file<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/web_view"/>
</LinearLayout>
That's enough with designing. -
Now, Get started with MainActivity.kt to produce functionality in your application.
- create variable to link WebView from activity_main.xml
private var mWebView : WebView = findViewById("R.id.web_view") - Your includes Javascript code, therefore to make your website work properly in your android application requires to enabled JavaScript
mWebView.settings.javaScriptEnabled = true - To open url in one over the other, you should have to create webViewClient Object
mWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
} - Finally add the loadUrl to the mWebView varibale to load your site on to your android application.
mWebView.loadUrl("https://www.getintocode.site/") - To move back to previous links by pressing back key, instead of closing your android application,
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack()
return true
}
return super.onKeyDown(keyCode, event)
}
- create variable to link WebView from activity_main.xml
< uses-permission android:name="android.permission.INTERNET" />
Now your app have the permission to use internet, without adding above permission to your manifest file, your app will not able to access internet.
No comments