androidkotlinanko

Android Anko Kotlin - Preload Views


I'm a bit new to Kotlin in Android, I've noticed that the Anko library supplies a nice tool for layout creation, for swiping the active view and defining the views with code.

I'm trying to use the Anko to define different template views to show async, but before showing a new View, I would like to preload it, for example a webpage or remote image etc.

I've not been able to find any info on if this is doable or not.

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        MyActivityUI().load("http://some.address.ip")
        val handler = Handler()
        handler.postDelayed({
            MyActivityUI().setContentView(this)
        }, 5000)
    }
}

class MyActivityUI : AnkoComponent<MyActivity> {
    lateinit var wv: WebView

    override fun createView(ui: AnkoContext<MyActivity>) = with(ui) {
        verticalLayout {
            wv = webView {
                webViewClient = WebViewClient()
            }.lparams(width = matchParent, height = matchParent)
        }
    }
}

This is partial but shown what I intend to do, so when the 5s timer is done, the webpage is already loaded to the View, instead of it starts to load

Any suggestions?


Solution

  • To answer your question exactly you could just keep a reference to the parent view and call addView to it and pass the webview in. However you might consider just setting the visibility to View.GONE/View.INVISIBLE I am not sure exactly what your goal here is. Also consider using the callback that determines the page finished loading rather than a generic 5 seconds. Hope this helps.