0
votes

My problem is that if there is a button on a website, then it will appear on the some page when the user click it . i want to enable function when the information is displayed . Please help me.

private WebView webView; private ProgressBar progressXml; private RelativeLayout WaitPageXml; // waiting page for user private RelativeLayout NoInternetPage;// page for no conection display for youser

protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);

     setContentView(R.layout.phone_number);

     webView = findViewById(R.id.WebViewXml);
     progressXml = findViewById(R.id.progressXml);
    WaitPageXml = findViewById(R.id.WaitPageXml);


    webView = findViewById(R.id.WebViewXml);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setInitialScale(15);
    WebSettings mWebSettings = webView.getSettings();
    webView.getSettings().setUseWideViewPort(true);
    WebSettings webSettings = webView.getSettings();
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setSupportMultipleWindows(true);
    webView.getSettings().setSupportZoom(true);
    mWebSettings.setAllowFileAccess(true);
    mWebSettings.setAllowFileAccess(true);
    mWebSettings.setAllowContentAccess(true);
    webView.addJavascriptInterface(new MyJavaScriptInterface(),
            "android");

webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap 
            favicon) {
                if (isOnline()) {
                    progressXml.setVisibility(View.VISIBLE);
                    WaitPageXml.setVisibility(View.VISIBLE);
                    super.onPageStarted(view, url, favicon);

                } else {
                    super.onPageStarted(view, url, favicon);
                    NoInternetPage.setVisibility(View.VISIBLE);
                }


            }

            @Override
            public void onPageFinished(WebView view, String url) {

                progressXml.setVisibility(View.GONE);
                WaitPageXml.setVisibility(View.GONE);
                NoInternetPage.setVisibility(View.GONE);

        });

webView.loadUrl("https://......");

@Override
public void onBackPressed() {

    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();

    }

}

public boolean isOnline() { //for test conection.... ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); @SuppressLint("MissingPermission") NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } else { return false; } }

unfortunately ,the rosponse was not executed by the site

1

1 Answers

0
votes

I'm having a bit of trouble telling exactly what you want from your question, but from how I interpret it, you need a button press in the web-page to trigger some sort of UI update (pop-up, field update, etc). There are a few different ways to do that, and I'm not entirely sure which is the best solution for what you're trying to accomplish.

However, given that you've posted this as an Android question and provided a source listing that includes your work for setting up the web view, I'm going to assume that you're looking for a way to hook up a button press from the HTML page to some Java callback. If that's the case, you're already part-way there. When you call webView.addJavascriptInterface(new MyJavaScriptInterface(), "android");, that injects an instance of MyJavaScriptInterface as a global JavaScript variable.

You don't have your implementation of MyJavaScriptInterface listed, so let's assume it's a class declared with the JavascriptInterface annotation, meant to be passed on to addJavascriptInterface. Annotation a class with JavascriptInterface exposes its public methods to JavaScript. Let's say you have your class declared as such:

@JavascriptInterface
class MyJavaScriptInterface {
    public void doSomething() {
        Log.d("MyJavaScriptInterface", "Got a callback from JavaScript");
    }
}

Since you added an instance of this class with the name "android" when you set up your WebView, then you can now call our doSomething function in JavaScript with android.doSomething(). For example, let's say this is the HTML that loads when you call webView.loadUrl("https://......");:

<html>
    <head>
        <script>
            function onButtonPressed() {
                android.doSomething();
            }
        </script>
    </head>
    <body>
        <button onclick="onButtonPressed();">Click me!</button>
    </body>
</html>

In that case, clicking the button on the page will call all the way down into doSomething in the Java class. From there, you can do whatever you need to do to display your information. Note that doSomething doesn't need to be a void; it can also return data, if you need displaying of information to be handled by JavaScript but retrieved natively.

For some more help, you can check out the Android developer guide on this topic here.