Home > Back-end >  Is it possible findViewById from WebView?
Is it possible findViewById from WebView?

Time:05-09

How can I get different view in WebView

or if web developer can tell me view's id?

class MainActivity : AppCompatActivity() {

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

        val webView = findViewById<WebView>(R.id.webView)
        webView.loadUrl("https://www.google.com")

//        Is it possible?
//        val button1 = webView.findViewById<Button>(R.id.button1)
//        val button2 = webView.findViewById<Button>(R.id.button2)
//
//        button1.setOnClickListener { fun1() }
//        button2.setOnClickListener { fun2() }
    }
}

Appreciate for any help, thanks.

CodePudding user response:

It is NOT possible to use findViewById for obtaining web-level widgets. It's for framework widgets only. There is no reliable way for handling web-based widgets on Java/Kotlin side in a way you want (just like native ones), but you can use some JS interfaces for calling click-related methods

CodePudding user response:

findViewById is not possible in webview instead of you can use Javascript interface

  webview.addJavascriptInterface(new Object()
  {
      @JavascriptInterface
    public void performClick()
    {
        Log.d("LOGIN::", "Clicked");
         Toast.makeText(MainActivity.this, "Login clicked", 
         Toast.LENGTH_LONG).show();
    }
  }, "login"); 
  • Related