I have initialized a ProgressBar in my MainActivity and assigned a view to it in onCreate. Inside my MainActivity I have created a WebViewClient class and I want to change the visibility of spinner inside it but for some reason I can't.

class MainActivity : AppCompatActivity() {
    lateinit var myWebView: WebView
    lateinit var spinner: ProgressBar
    override fun onCreate(savedInstanceState: Bundle?) {
        spinner = findViewById(R.id.progressBar1)
        myWebView = findViewById(R.id.webview)

        myWebView.webViewClient = MyWebViewClient()

    private class MyWebViewClient : WebViewClient() {
        override fun onPageFinished(view: WebView, url: String?) {
            super.onPageFinished(view, url)
            view.visibility = View.VISIBLE

            //Here I want to change the visibility of spinner

2 Answers

Xite On Best Solutions

You should use inner class instead of nested class if you want to access members of outer class. See: https://kotlinlang.org/docs/reference/nested-classes.html.

In your example you should add inner:

private inner class MyWebViewClient : WebViewClient()

Be aware that it's easy to introduce a memory leak using inner classes, as every inner class contains a reference to outer class.

serg3z On

Your code should work, most likely the problem is in the progressView in xml, it is set to match_parent and just overlaps your webView.
Try after view.visibility = View.VISIBLE add spinner.visibility = View.GONE