Posts

[ KOTLIN ] WebView With Url option || ANDROID STUDIO


WATCH VIDEO TUTORIAL :


Step 1 :
  • Create New Project With EmptyActivity
Step 2 :
  • Add EditText , Button And WebView as follow :
  • <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="GO" />
    
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:ems="10"
        android:hint="Type Your URL Here"/>
    
    <View
        android:id="@+id/view12"
        android:layout_width="wrap_content"
        android:layout_height="3dp"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:background="#3bbdfa" />
    
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true" />
Step 3 : 
  • Add code in kotlin file :
      button1.setOnClickListener {
            var Address = "http://" + editText1.text.toString()
            val webSetting = webView1.settings
            webSetting.builtInZoomControls = true
            webSetting.javaScriptEnabled = true
            editText1.visibility = View.GONE
            button1.visibility = View.GONE
            webView1.webViewClient = WebViewClient()
            webView1.loadUrl(Address)
        }
    }

        override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
            if (keyCode == KeyEvent.KEYCODE_BACK && webView1.canGoBack()) {
                val pulse = AnimationUtils.loadAnimation(this, R.anim.abc_slide_out_bottom)
                webView1.startAnimation(pulse)
                webView1.goBack()
                editText1.visibility = View.VISIBLE
                button1.visibility = View.VISIBLE


                return true
            }
            return super.onKeyDown(keyCode, event)

Step 4 : 
  • Add internet permission in manifest :
  •     <uses-permission android:name="android.permission.INTERNET" />
  • Run Your App
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.