1

I have taken reference to this previously asked question. I have tried doing this

public class MainActivity extends AppCompatActivity {

WebView webview;

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

    webview=findViewById(R.id.webview1);

    Toast.makeText(MainActivity.this, "Loading...", Toast.LENGTH_SHORT).show();

    String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
    webview.getSettings().setUserAgentString(newUA);

    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient());
    webview.loadUrl("https://facebook.com");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (webview.canGoBack()) {
                    webview.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

}

and did what an answer suggested, but as in the comments of one of the answer to the reference question, it asks me to update my browser. My aim is to open the "Whatsapp-Web" desktop site inside my webview. What should I do regarding the browser and any advice regarding opening whatsapp. Thank you in advance.

Edit: This is the new Code that I am trying with a new User Agent

String newUA= "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
    whatsapp.getSettings().setUserAgentString(newUA);

    String DESKTOP_USERAGENT = whatsapp.getSettings().getUserAgentString ();
    DESKTOP_USERAGENT = DESKTOP_USERAGENT.replace("Mobile ","");
    whatsapp.getSettings().setUserAgentString(DESKTOP_USERAGENT);

    whatsapp.getSettings().setJavaScriptEnabled(true);
    whatsapp.setWebViewClient(new WebViewClient());
    whatsapp.loadUrl("https://web.whatsapp.com/");
J.Doe
  • 23
  • 8
  • Firefox 4 sounds like an old browser. Get a different user agent. You can get one by opening your regular browser on pc then visit https://www.whatismybrowser.com/detect/what-is-my-user-agent – Zun Jun 26 '18 at 07:27
  • Also, the StackOverflow link you looked at is **5 years old**. Check the date whenever you use a link as reference – Zun Jun 26 '18 at 07:28
  • I tried many other links on the site but this was the one that was the closest to a working state, hence i used this question. – J.Doe Jun 26 '18 at 07:30
  • I tried many different kinds of user strings but none worked. they just opened the mobile site. I was looking for a different approach maybe, if that worked. – J.Doe Jun 26 '18 at 07:32
  • Have you already tried what I suggested? Use a modern desktop user agent? – Zun Jun 26 '18 at 07:33
  • yes, but it opened mobile sites only.. could you give me a newer user agent if you have one that might open desktop site?? Maybe the ones I tried were not correct or something.. – J.Doe Jun 26 '18 at 07:48
  • Can you show me what user agent you are using? – Zun Jun 26 '18 at 07:50
  • isnt it the one in the code?? Im new to this.. – J.Doe Jun 26 '18 at 08:07
  • I told you to click the link I provided. It's not difficult, all you have to do is click on it, then the site will display a newer user-agent for you. – Zun Jun 26 '18 at 08:08
  • ohh inside that.. in that I used "Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; SCH-I535 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"... and it worked but only for mobile sites.. but it did not direct to desktop sites... is there a problem you see that I might have missed?? – J.Doe Jun 26 '18 at 08:16

1 Answers1

0

You can use WebView to show view as Desktop Site with fit in mobile display.

    webView = (WebView)findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);

    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);

    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);

Let me know if this solution works

anubysh
  • 576
  • 6
  • 18