- Kotlin Android – WebView – Example
- Kotlin Android – WebView
- Example – Android AlertDialog
- Conclusion
- WebView Example in Android (Kotlin)
- Get Solution Code
- Android WebView app Example in Kotlin code
- Let’s build an Android webView application
- Simply Adding a WebView to Your Application
- Enabling JavaScript
- Navigating web page history
- More web view settings
- Complete code
- Output screenshot android web view app :
- Download Link and Source code Android WebView in Github
- Android studio kotlin webview example
- About
- Android WebView in Kotlin with Examples
- Android WebView in Kotlin Example
- Load Html file into WebView
- Load html string in a WebView
- Load external webpage inside WebView
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
Step 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.
Step 2: Include Android WebView In layout fileYou can include following WebView snippet in Layout file to display WebView in Activity.
Step 3: Load URL in WebView programmatically in Activity file.
Example – Android AlertDialog
In this Example Kotlin Android Application, we shall display a WebView and load “www.google.com url”.
Create a Kotlin Android Application with Empty Activity and modify the following files.
AndroidManifest.xml
activity_main.xml
MainActivity.kt
Run this Android Application, and you will get a WebView embedded in the Application as shown in the following screenshot.
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.
Источник
WebView Example in Android (Kotlin)
Welcome Guys, In this WebView Example, we’ll learn how to build web apps using WebView in Android. Android WebView mostly used to display web pages as a part of your app. WebView is a subclass of View. It specially wrote for showing a webpage in your activity.
In this article, we’ll create a sample application, that contains WebView with ProgressBar. We’ll see how to handle back press event as well
Uses of WebView
WebView is very helpful if you want to show some HTML and javascript content in-app. Such as end-user agreement or a user guide.
WebView Sample App
1 Create a new project
Let’s open the AndroidStudio and create a new project with some default template. Now open the android manifest file and add uses permission for internet
2. Let’s create WebActivity
Navigate the file menu and create a new activity with a layout file named is WebActivity. Open the layout file and paste the below code. This layout file we are taking web view and progress bar
3. Let’s move to WebActivity
In source file initialized WebChromeClient using below code. webview settings you can changes as uses. One more thing that I do in this block of code is override onReceivedSslError.
4. Set WebChromeClient to WebView
In this crome client, we override onProgressChanged() methods. Based on the progress we are showing and hiding progress bar
5. Manage web page history
6. Create loadUrl function
7. Finally, the full source of WebActivity looks like below
This is a full source of this activity, Still, you are missing some things I’ll add source code on below as well
Conclusion
All Done. In this android webview example, we learned webview uses, implementation, and manage webpage history, I hope it’s helpful for you, then help me by sharing this post with all your friends
Get Solution Code
If you have any queries please put your comment below.
Источник
Android WebView app Example in Kotlin code
With an Android WebView, you can show the website in an android application. Web-view is containers like scrollView, ListView, etc in Android app development. You can add it using Android studio drag-drop or in XML code directly. The purpose of WebView is if you want a web application or just a web page in your app
In Android WebView class is an extension of Android’s View class that allows you to display web pages as a part of your activity layout or other resources layout.
Let’s build an Android webView application
In this example, we are using Kotlin language with WebView and showing Responsive website in mobile. Also, we will add “If web view has back history, then go to the web view back history”.
Simply Adding a WebView to Your Application
To add a WebView to your Application, simply include the element in your resource layout file. For example, here’s a layout file in which the WebView match the screen, if you are suing LinearLayout or Relative Layout for ConstraintLayout you have to set with GUI builder (drag and drop).
To load a web page in the WebView , use loadUrl() . For example:
Before this will work, however, your application must have access to the Internet. To get Internet access, request the INTERNET permission in your manifest file. For example:
Enabling JavaScript
JavaScript is disabled in a WebView by default. If your website is using JavaScript (Nowadays every side using) then you must have to enable JavaScript.
// Get the web view settings instance
val setting = webview.settings;
// Enable javascript in web view
Navigating web page history
When you go inside webpage to webpage and then click Back button app will close. So you can override back button override fun onBackPressed() and Handle back press.
More web view settings
You can handle more setting of web view like :
- Enable javascript in web view
- Enable and setup web view cache
- Enable zooming in web view
- Zoom web view text
- Enable disable images in web view
- safeBrowsingEnabled
Complete code
Create an android project in the android studio (Follow this tutorial: Android First Program in Android Studio)
Add WebView in main_activity.xml, used ConstraintLayout
Add fowling code in MainActiviyt.xml, the code in Kotlin, know more about basic fundamentals of kotlin
Note: Don’t forget to add internet permission in AndroidManifest.xml
It will throw an error or not upload webpage in the app.
Output screenshot android web view app :
Download Link and Source code Android WebView in Github
Note : This example (Project) is developed in Android Studio 3.0.1 ,tested on Android 7.1.1 ( Android Nougat), compile SDK version API 26: Android 8.0 (Oreo)
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.
Источник
Android studio kotlin webview example
Android WebView Example with Kotlin. Website to Native Android App Template.
An Android WebView developed in Kotlin . This project will help to develop responsive website to Android app. This project is open source so if you want to contribute contact me.
What is Android web view? Android web view is web-based content which came from “android.webkit” library. The concept of web view is its load offline or online webpage.
Online web view: If you have a website which is responsive and runs well on the mobile browser then using web view you can develop android or ios app. Which load the URL and fill like Android app.
Offline web view: If you want to load the webpage in offline then the logic keeps the file in your asset folder and load the offline page. But in that case, you can’t use the server database and can’t update the app without publishing.
Native app vs cross-platform web view app? Web view app possible to develop in both platform. Both app main concept is the same.
Native APP: Native app is official to provide a platform. Android official platform is Android studio which uses Java or Kotlin language. Because of it’s official so the resource is available and you get some advantage of using it.
Cross-platform: Cross app is an unofficial platform. Xamarin & flutter one of them. The benefit is you get Android and iOS both app from one platform. But the resource is limited some time it’s paid. From my experience I suggest must use Native app because of it’s official and in future if you need to add anything then easy to integrate.
Is it possible to change web view UI? The web view is loading your website. So all it’s show that you have on your website. Not possible to change anything inside the web view. But if you can change the app color, add multiple pages, use AdMob, firebase, one signal notification, etc.
How to use it? Just set your website url on MainActivity.kt as URL.
Get Android Development with Kotlin Book: https://amzn.to/35ZA3ab
RocketWeb is the pro version of the webview, where availabe multiple color control and theme, see live example:
https://play.google.com/store/apps/details?id=infix.imrankst1221.rocket.web
For better experience and customiation use RocketWeb:
https://codecanyon.net/item/rocketweb-android-web-app-solution-webtoapp/22985174
Contact with me
Buy me a Coffee. ☕️
About
Native Android WebView Example in Kotlin. Website to android app github open source template.
Источник
Android WebView in Kotlin with Examples
In this tutorial, we are going to create an Android WebView in Kotlin with the help of examples. Android WebView is another UI widget that is used to load web pages within the application.
Android WebView is a subclass of AbsoluteLayout. WebView has loadUrl() and loadData() methods to load and display web pages and Html content.
There are different ways to load web pages and custom HTML. In this tutorial, we will try to read Html content as String, load Html file into WebView and load external webpage inside WebView.
Before we start open AndroidManifest.xml and provide the internet permission.
Android WebView in Kotlin Example
To start Android Kotlin WebView, first of all let’s setup the WebView in the XML file.
activity_main.xml
Some of the XML properties that are used in activity_main.xml
XML Attribute | Description |
---|---|
android:id | Id is the unique identifier |
Load Html file into WebView
To load a local html file into the WebView, let’s create a small webpage page using plain html and css. If you have already designed your web page then you can simply follow the loading steps.
1st create an Assets folder and place the Html, CSS and image inside it. Then using the loadUrl function load the local Html page.
The Html file name is city.html, the CSS file name is main.css and image that is used inside the city.html is lahore.jpg.
main.css
city.html
MainActivity.kt
Inside the MainActivity.kt we are simply loading the local Html page using the WebView webview.loadUrl method.
Run the app and see the output.
Cool now you can make a mobile app using html and css.
Load html string in a WebView
What if we have some html content and want to load as a String using Kotlin and WebView.
MainActivity.kt
We have an Html heading H1 with the text “Welcome Android-Kotlin.com”. Stored the H1 in message variable. Defined the mimeType and utfType. Later displayed using loadData() function.
If we run the app again we will have new result on the screen.
Load external webpage inside WebView
Now the most interesting part, how to load external webpage inside the Webview in android using the Kotlin as a programming language.
We can load a website in Webview and it will behave like an Android app. To do so we have to create an WebViewClient class. It has a method called shouldOverrideUrlLoading which is used to load the URL within the app’s web view otherwise android will try to open the pages with an external browser.
MainActivity.kt
Run the app once again to see the magic.
That is all from Kotlin Android WebView tutorial. If you have any question please don’t hesitate to ask.
You can also learn about how to create custom Toast in Android.
Источник