WebView In Android

·

1 min read

While Building Your App, You May Have To Use Content/Webpage From The Internet; In That Case, WebView Can Help You. -This Blog Aims To Implement A Web Page In Your Android App, Let's Get Started.

->Before Proceeding To Further Make Sure That You Have Added Internet Permission Tag In Manifest File If You Didn't; You Can Access Internet By Adding The Following Attribute/Tag In Your Project Manifest File👇🏻


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

In Your XML File👇🏻


<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Note:- I am Using Constraint Layout In .XML File, Depending On Your Layout You Can Change The Attributes.

In Your Kotlin File👇🏻


val webView=findViewById<WebView>(R.id.webView)
webView.settings.javaScriptEnabled=true
webView.webViewClient=object :WebViewClient(){
override fun shouldOverrideUrlLoading(
view: WebView?,
url: String?
): Boolean {
view?.loadUrl(url!!)
return true
}
}
webView.loadUrl("https://saketh001.hashnode.dev")

Explanation: WebViewClient:- When User Interacts/Clicks Something in Your WebView, User Will Be Redirected To Default Browser In Their Mobile, To Avoid This WebViewClient Can Be Used, It Shows The Whole WebPage/URL Which You Provided In-App Itself Rather Than Redirecting To Default Browser. javaScriptEnabled:-By Enabling JavaScript WebPage Which You Want To Include Will Be Fully Functional In-App.