Home > Blockchain >  Need to update textview with url everytime when webview url changes in kotlin
Need to update textview with url everytime when webview url changes in kotlin

Time:07-15

Trying make a simple browser, and can't set a fild that will showing current webview's url. Tried override functions, but always says that "name of function" overrides nothing. Also can add that webview is within fragment. Maybe i can call this from mainactivity, but idk how. webview_fragment.kt

package com.example.webviewapp

import android.content.ContentValues.TAG
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment


class webview_fragment : Fragment(){


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?

    ): View? {
        val view : View = inflater.inflate(R.layout.layout_webview_fragment, container, false)
        val  mWebView : WebView = view.findViewById(R.id.webViewMain)
        mWebView.loadUrl("https://google.com")
        val webSettings = mWebView.getSettings()
        webSettings.setJavaScriptEnabled(true)
        mWebView.setWebViewClient(WebViewClient())
        requireActivity()
            .onBackPressedDispatcher
            .addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    Log.d(TAG, "Fragment back pressed invoked")
                    if (mWebView.canGoBack()){
                        mWebView.goBack()
                    } else {
                        requireActivity().finishAndRemoveTask()
                    }
                }
            }
            )
        mWebView.webViewClient = object : WebViewClient(){
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                val urlText: TextView? = view?.findViewById(R.id.urlText)
                val  mWebView : WebView? = view?.findViewById(R.id.webViewMain)
                urlText?.setText(mWebView?.getUrl())
            }
        }
        return view
    }
}

mainactivity.kt

package com.example.webviewapp

import android.content.ContentValues.TAG
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        
        if (getSharedPreferences("app", Context.MODE_PRIVATE).getBoolean("Boolean", false) == false) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.fullcreen_holder, start_fragment())
                .commit()
        } else {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.fullcreen_holder, webview_fragment())
                .commit()
        }
    }
}

CodePudding user response:

You can simply get the web url from the webView like this

binding.webView.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                Log.d("onPageStarted ","${view?.url} ")
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                Log.d("onPageFinished ","${view?.url} ")
            }
        }

There are few issues in your code

val view : View = inflater.inflate(R.layout.layout_webview_fragment, container, false)

mWebView.webViewClient = object : WebViewClient(){
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            val urlText: TextView? = view?.findViewById(R.id.urlText)
            val  mWebView : WebView? = view?.findViewById(R.id.webViewMain)
            urlText?.setText(mWebView?.getUrl())
        }
    }

In the above code the textView you are trying to access it from the WebView's view instead from your fragment's view that's why you are facing the issue, you should rename the reference to the webView in override fun onPageFinished(view: WebView?, url: String?) to resolve the conflict.

CodePudding user response:

You can simply do it like this,

YOUR_WEBVIEW.apply {
        webChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView?, title: String?) {
                super.onReceivedTitle(view, title)
                YOUR_TEXTVIEW.text = title.toString()
            }
        }
}

it will update the text view automatically every time you move to another web page.

  • Related