Reputation: 1187
I got a website and I want the website to become a mobile app since it is responsive. I decided to make it as a WebView since it is the easier to do because I am not a mobile dev. My website uses Laravel + Vuejs SPA (single page application). But after I log in when I click to the navbar, It does not redirect me to the next page. Can someone tell me what I missed? Does it concern my website or its just an android problem?
MainActivity.java
package com.example.carrental;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebViewClient(new Callback());
web.loadUrl("http://52.74.70.6:90/");
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return false;
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.carrental">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:networkSecurityConfig="@xml/network_security_config"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0
Views: 1107
Reputation: 389
you can watch your url redirection when website open in web view by below code
public class PaymentWebActivity extends Activity {
private WebView webView;
private String link = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paymentview);
webView = (WebView) findViewById(R.id.webview);
if (getIntent().getExtras() != null) {
link = getIntent().getExtras().getString("link");
}
initWebView();
}
private void initWebView() {
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl(link);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public class WebViewClientImpl extends WebViewClient {
public WebViewClientImpl(Activity activity) {
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.indexOf(link) > -1) return false;
Log.e("Url :", url);
checkUrl(webView, url);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
if (webResourceRequest.getUrl().toString().indexOf(link) > -1) return false;
Log.e("Url :", webResourceRequest.getUrl().toString());
checkUrl(webView, webResourceRequest.getUrl().toString());
return false;
}
}
private void checkUrl(WebView webView, String url) {
if (checkSuccessUrl(url)) {
webView.destroy();
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK,returnIntent);
finish();
} else if (checkErrorUrl(url)) {
webView.destroy();
Uri uri = Uri.parse(url);
String errorMessage = uri.getQueryParameter("message");
showConfirmDialog(errorMessage);
}
}
private void showConfirmDialog(String errorMessage) {
final Dialog dialog = new Dialog(PaymentWebActivity.this);
Window w = dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_error);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(w.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
w.setAttributes(lp);
w.setBackgroundDrawableResource(android.R.color.transparent);
TextView lbltitle = (TextView) w.findViewById(R.id.tvTitle);
lbltitle.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, "APP Name"));
lbltitle.setTypeface(CommonFunctions.getInstance().getRegular(PaymentWebActivity.this));
TextView lblmessage = (TextView) w.findViewById(R.id.tvMessage);
lblmessage.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, errorMessage));
lblmessage.setTypeface(CommonFunctions.getInstance().getRegular(PaymentWebActivity.this));
Button btnOk = (Button) w.findViewById(R.id.btnOk);
btnOk.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, "OK"));
btnOk.setTypeface(CommonFunctions.getInstance().getBold(PaymentWebActivity.this));
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
finish();
}
});
Button btnCancel = (Button) w.findViewById(R.id.btnCancel);
btnCancel.setVisibility(View.GONE);
/*btnCancel.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, "Cancel"));
btnCancel.setTypeface(CommonFunctions.getInstance().getBold(PaymentWebActivity.this));
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});*/
dialog.setCancelable(false);
dialog.show();
}
private boolean checkSuccessUrl(String url) {
return url.contains("success.php");
}
private boolean checkErrorUrl(String url) {
return url.contains("error.php");
}
}
these two files are created on a server side so when checkUrl found this files we can execute our logic as per the url. you can also forward page as per the found.
Upvotes: 1