Home » Android » javascript – Clicks Does Not Working in WebView

javascript – Clicks Does Not Working in WebView

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a webview in my android app.
I have loaded the url: https://imsnsit.org/imsnsit/notifications.php
There are links to various notifications. My WebView is doing nothing when I click on them.
It is working on everything else – Chrome (Android), Chrome(Desktop). The links are fine. One thing I notice notifications have the href with PHP file. Just navigating to that link does not work. I get Invalid operation. Plus the links are not static, they change everytime you refresh the page(only just the parameter for plum_url.php).
I am already using these functions. Nothing helps.
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setDomStorageEnabled(true);

How to&Answers:

I checked your link and all the links are opening a pdf file which is not supported in android web view. you can open such links in google docs or maybe some app in you device.
Implement shouldOverrideUrlLoading something like this.

 @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if ( urlIsPDF(url)){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "application/pdf");
        try{
            view.getContext().startActivity(intent);
        } catch (ActivityNotFoundException e) {
            //user does not have a pdf viewer installed
        }
    } else {
        webview.loadUrl(url);
    }
    return true;
}

Answer:

you can open new window in webview like this

WebView webviewContact=(WebView)findViewById(R.id.webviewcontact);  

webviewContact.loadUrl("https://imsnsit.org/imsnsit/notifications.php");

webviewContact.clearCache(true);

webviewContact.clearHistory();
//webviewContact.getSettings().setDomStorageEnabled(true);
//webviewContact.getSettings().setSupportMultipleWindows(true);
webviewContact.getSettings().setJavaScriptEnabled(true);        webviewContact.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webviewContact.setWebViewClient(new WebViewClient(){
/*For open new window in webview*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {                                    Toast.makeText(getApplicationContext(),url,Toast.LENGTH_SHORT).show();
                view.loadUrl(url);
                return false;
            }  
        });

Answer:

https://www.mkyong.com/android/android-webview-example/

Create two Android layout files – “res/layout/main.xml” and “res/layout/webview.xml“.

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to http://www.google.com" />

</LinearLayout>

File : res/layout/main.xml – WebView example

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

Two activity classes, an activity to display a button, another activity display the WebView with predefined URL.

File : MainActivity.java

package com.mkyong.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {
        final Context context = this;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonUrl);

        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(context, WebViewActivity.class);
            startActivity(intent);
          }

        });

    }

}

File : WebViewActivity.java

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");

    }

}

WebView required INTERNET permission, add below into AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />

File : AndroidManifest.xml – See full example.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".WebViewActivity"
            android:theme="@android:style/Theme.NoTitleBar" />

        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

it will help you.

https://www.mkyong.com/android/android-webview-example/

Answer:

I would suggest you start using custom tabs its faster and better. The detailed implementation is at https://developer.chrome.com/multidevice/android/customtabs

// Use a CustomTabsIntent.Builder to configure CustomTabsIntent.
// Once ready, call CustomTabsIntent.Builder.build() to create a CustomTabsIntent
// and launch the desired Url with CustomTabsIntent.launchUrl()

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

Custom tabs would be a great solution if you are planning no redirection to apps from your web view. The problem with webview is very specific to your implementation so cant suggest a soln here. Kindly paste the implementation of how you are opening a web view and what is the php content or the links that you are trying to open.