Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
rmanalo

Draggable works only once on Android WebView

Recommended Posts

I tried to place my page using Draggable.js on an Android WebView but it seems like it's not working after I dragged something.  It works on the Chrome app though.

 

This is my WebSettings for the WebView.

 

String UA = "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19" +
                    " (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19";

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setSupportZoom(false);
settings.setBuiltInZoomControls(false);
settings.setPluginState(android.webkit.WebSettings.PluginState.ON_DEMAND);
settings.setDatabaseEnabled(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setDomStorageEnabled(false);
settings.setUseWideViewPort(true);
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);

settings.setUserAgentString(UA);

 

This is my whole code.

 


 

import android.content.Context;
import android.content.DialogInterface;
import android.net.http.SslError;
import android.os.Build;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    WebView webView;
    SwipeRefreshLayout swiLyt;
    String url = "";
    Context ctx = this;
    String TAG = "simpler";
    String httpReq = "";
    TextView lbl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webview);
        swiLyt = findViewById(R.id.swiLyt);
        lbl = findViewById(R.id.lbl);

        httpReq = "http://";

        swiLyt.setEnabled(false);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webView.setWebContentsDebuggingEnabled(true);
        }

        final WebViewClient webClient = new WebViewClient(){

            @Override
            public void onPageFinished(WebView view, String url) {
                swiLyt.setRefreshing(false);
                lbl.setText(url);

                fullScreen();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                AlertDialog.Builder dlgBuild = new AlertDialog.Builder(ctx);
                dlgBuild.setTitle("Error");
                dlgBuild.setMessage("Err: "+errorCode+" desc: "+description);
                dlgBuild.setCancelable(false);
                dlgBuild.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                AlertDialog.Builder dlgBuild = new AlertDialog.Builder(ctx);
                dlgBuild.setTitle("Error");
                dlgBuild.setMessage("Err: "+error.toString());
                dlgBuild.setCancelable(false);
                dlgBuild.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                super.onReceivedError(view, request, error);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                AlertDialog.Builder dlgBuild = new AlertDialog.Builder(ctx);
                dlgBuild.setTitle("SSL Error");
                dlgBuild.setMessage("Err: "+error.toString());
                dlgBuild.setCancelable(false);
                dlgBuild.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                super.onReceivedSslError(view, handler, error);
            }

        };

        WebChromeClient webChromeClient = new WebChromeClient() {

            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                AlertDialog.Builder dlgBuild = new AlertDialog.Builder(ctx);
                dlgBuild.setTitle("Console");
                dlgBuild.setMessage(consoleMessage.message());
                dlgBuild.setCancelable(false);
                dlgBuild.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });

                return super.onConsoleMessage(consoleMessage);
            }
        };

        String UA2 = "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36" +
                " (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36";

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        settings.setPluginState(android.webkit.WebSettings.PluginState.ON_DEMAND);
        settings.setDatabaseEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setDomStorageEnabled(false);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);

        settings.setUserAgentString(UA2);

        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.setWebViewClient(webClient);
        webView.setWebChromeClient(webChromeClient);

        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);

        webView.setLongClickable(true);
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Log.d(TAG, "long click");

                return false;
            }
        });

        webView.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                return false;
            }
        });
        
        showDlg();

    }

    public void showDlg(){
        RelativeLayout lyt = new RelativeLayout(ctx);
        final EditText txt = new EditText(ctx);
        RelativeLayout.LayoutParams lytParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        txt.setHint("ip:port");
        txt.setLayoutParams(lytParams);
        lyt.addView(txt);
        txt.setText("192.168.149.59/pos_mock_up");

        AlertDialog.Builder dlgBuild = new AlertDialog.Builder(ctx);
        dlgBuild.setTitle("Enter ip:port");
        dlgBuild.setCancelable(false);
        dlgBuild.setView(lyt);
        dlgBuild.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String ipPort = txt.getText().toString();
                if(!ipPort.contains(httpReq)){
                    url = httpReq+ipPort;
                }
                Log.d(TAG, "url: "+url);
                swiLyt.setRefreshing(true);
                webView.loadUrl(url);
            }
        });
        dlgBuild.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        AlertDialog dlg = dlgBuild.create();
        dlg.show();
    }

    @Override
    public void onBackPressed() {
        webView.loadUrl("javascript:OnBackPressed()");
    }

    private void fullScreen(){
        final View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        decorView.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        // Note that system bars will only be "visible" if none of the
                        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            // The system bars are visible. Make any desired
                            // adjustments to your UI, such as showing the action bar or
                            // other navigational controls.
                            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
                        } else {
                            // The system bars are NOT visible. Make any desired
                            // adjustments to your UI, such as hiding the action bar or
                            // other navigational controls.
                            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
                        }
                    }
                });
    }
}

 

Sorry, I don't really know how to create a test but that code above is everything I only used for the project I'm working.

 

This is my layout file if you need it.

 

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiLyt"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        a<WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        </WebView>
    </android.support.v4.widget.SwipeRefreshLayout>

    <TextView
        android:id="@+id/lbl"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#0000ff"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>

 

Link to post
Share on other sites

Sorry but it's very difficult to troubleshoot blind - do you have a reduced test case, perhaps in a codepen? And for the record, I have no idea what to do with any of that WebView setting data. I know Draggable works well on many Android devices and this is the first I'm hearing of any issues recently, so I wonder if there's something else going on in your code(?)

Link to post
Share on other sites

Hello. I'm sorry for not being able to provide a test case. I have no knowledge about writing it. I provided my whole code though.

Link to post
Share on other sites

Hm, the code you provided doesn't have any GreenSock-related code/tools. I wish I could help more. 

Link to post
Share on other sites

Oh sorry. The page that the webview loads uses TweenMax, Draggable and CSSPlugin. I'm the one who tried the page on a WebView and found this problem.

I posted this question here because I think the problem might be something about me using the wrong WebSettings for my WebView because the page works fine on the Chrome app and not on the WebView that uses the same engine as the Chrome app.

Link to post
Share on other sites

Ah, okay. Well, this probably isn't the best place for that type of question. We're happy to answer GreenSock-related questions, but I have no idea what WebView quirks there are or Android-specific implementation details like that. I sure wish I had a better answer for you.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×