Web View
The web view is used to load the website directly into our app.
To load the website, we required internet permission. This INTERNET permission
should be added in the manifest file.
1.AndroidManifest.xml Before the application tag
<uses-permission android:name=”android.permission.INTERNET”/>
2.activity_main.xml
<LinearLayout
android:orientation=”vertical”>
<RelativeLayout
android:layout_width=”wrap_content”
android:layout_width=”match_parent”>
<Button
android:id=”@+id/b1”
android:layout_width=”48dp”
android:layout_height=”warp_content”
//We can’t specify the < or > symbols directly. Right-click bulb Extract string
resource Resource name =”back” Ok. Now android:text=”@string/back”
comes into the xml file.
android:text=”<”/>
<Button
android:id=”@+id/b2”
android:layout_width=”48dp”
android:layout_height=”warp_content”
//We can’t specify the > symbol directly. Right-click bulb Extract string resource
Resource name =”next” Ok. Now android:text=”@string/next” comes into
the xml file.
android:text=”>”/>
<Button
android:id=”@+id/b3”
android:layout_width=”48dp”
android:layout_height=”warp_content”
//We can’t specify the > symbol directly. Right-click bulb Extract string resource
Resource name =”go” Ok. Now android:text=”@string/go” comes into the
xml file.
android:text=”go”/>
<EditText
android:id=”@+id/et1”
android:layout_width=”48dp”
android:maxLines=”1”
android:layout_height=”warp_content”/>
</RelativeLayout>
<WebView
android:id=”@+id/wv1”
android:layout_width=”match_parent”
android:layout_height=”warp_content”/>
</LinearLayout>
3. MainActivity.kt
class MainActivity : AppCompatActivity()
lateinit var url:EditText
lateinit var btngo:Button
latinit var btnback:Button
lateinit var btmforward:Butoon
lateinit var web:WebView
override fun onCreate(b:Bundle?)
super.onCreate(b)
setContentView(R.layout.activity_main)
url = findViewById(R.id.et1) as EditText
btngo = findViewById(R.id.b3) as Button
btnnext = findViewById(R.id.b1) as Button
btnforward = findViewById(R.id.b2) as Button
web = findViewById(R.id.wv1) as WebView
btngo.setOnClickListener({
web.loadUrl(“https:// “ + url.text.toString())
})
btnback.setOnClickListener({
if(web.canGoBack())
web.goBack()
else
Toast.makeText(this,”No history available”,Toast.LENGTH_LONG).show()
})
btnforward.setOnClickListener({
if(web.canGoForward())
web.goForward()
else
Toast.makeText(this,”No history available”,Toast.LENGTH_LONG).show()
})
Output: The web page will be open in the default browser.