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>