Help me to solve my exercise. I make WebView app that showing WhatsApp Web QR Code in Desktop Mode, QR Code not showing in Mobile View.
I have success to open WhatsApp Web in WebView in Desktop Mode. But when you click a link in it, it changes to Mobile View. My idea is to open link in browser, not in WebView. Everythings fine, but when you close the app then open it again, it directly open WhatsApp Web in browser.
This is my app source code, including :
- progress bar
- ads
- desktop mode (user agent string)
- open link in browser
Can you help me?
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class Dashboard extends AppCompatActivity {
private WebView myWebView;
private ProgressBar progressBar;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
progressBar = findViewById(R.id.progressbar_in);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://web.whatsapp.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setDomStorageEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
webSettings.setDisplayZoomControls(false);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(false);
//myWebView.setWebViewClient(webViewClient);
myWebView.setWebViewClient(new MyWebViewClient());
String userAgent = webSettings.getUserAgentString();
try {
String androidString = webSettings.getUserAgentString().
substring(userAgent.indexOf("("), userAgent.indexOf(")") + 1);
userAgent = webSettings.getUserAgentString().replace(androidString, "X11; Linux x86_64");
} catch (Exception e) {
e.printStackTrace();
}
webSettings.setUserAgentString(userAgent);
myWebView.reload();
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
// WebViewClient webViewClient = new WebViewClient() {
public final class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Intent i = new Intent(Intent.ACTION_VIEW, request.getUrl());
startActivity(i);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}