GingerGirl GingerGirl - 6 months ago 27
Android Question

How to keep responsive layout in WebView Android app?

I have created an Android app using

WebView
. This app shows the content of a website. There are many internal links and external links (with a 301 redirect). Internal links are correct, but when I tap on external links, I see a fullscreen desktop layout instead of the responsive version of the page and it looks very bad.

How do I edit the following code in order to always get the responsive layout to fit the screen?

webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(final WebView view, String url) {
// hide loading image
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
}
}, 1000);
}
@Override
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(description).setPositiveButton(getText(R.string.ok), null).setTitle("onReceivedError");
builder.show();
}

});

// load url (if connection available
if (isInternetConnected(this)) {
String URL = "http://www.example.com/";
webView.loadUrl(URL);
}else{
// showAlertDialog(this, "No Internet Connection",
// "You don't have internet connection.", false);
new AlertDialog.Builder(this)
.setTitle("No Internet Connection")
.setMessage("You don't have internet connection.")
.setCancelable(false)
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// whatever...
finish(); //Close current activity
startActivity(getIntent()); //Restart it

}
}).create().show();
}

Answer

Actually you are testing on a tablet and amazon website looks similar to desktop version from a tablet, while it becomes responsive when browsed from a phone.

Please try from a standard phone and you'll see that its mobile version of the site :)

Please let me know in case your problem isn't solved.

Thanks

Sanskar

Comments