WebView size in android studio - java

when I run my material design site in Chrome, it is displayed like:
But when I run in WebView, it is displayed like
I want to display it in WebView like it does in Chrome
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView)findViewById(R.id.webView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("javascript:document.body.style.zoom = "+String.valueOf(scale)+";");
myWebView.loadUrl("http://site");
}

Related

WebView Android Studio

I have created a WebView of this website https://smartfix.pk/ but in my app, WhatsApp is not working,
when I click it gives me the error
webpage at whatsapp://send/?phone=923005475032&text=Hello%2C%20I%20have%20a%20question%20about%20SmartFix%20Services could not be loaded because: net::ERR_UNKNOWN_URL_SCHEME
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("https://smartfix.pk/");
}
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return false;
}
}
}
try to add this, i'm not sure it's will work for that page but when i'm using webview i always enable all to be sure
webSettings.setLoadsImagesAutomatically(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
}

get content html page with web view in android

I am working on an Android project in which I am loading an HTML page from the internet, not locally and then displaying it in WebView. It works fine, but unfortunately, when I click on any of the element in WebView, it opens the link in Browser.
What changes do I need to make so that any links clicked within the web-view will be opened by Web-view itself. Thank you.
public class ContainerActivity extends Activity {
private WebView mWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contain);
mWebView = (WebView)findViewById(R.id.wvPortal);
mWebView.loadUrl("www.domain.com/path/to/index.html");
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
}
}
Try to use setWebViewClient:
mWebView = (WebView)findViewById(R.id.wvPortal);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("www.domain.com/path/to/index.html");

Android Webview app shows black screen when i open it

Android weview app I compiled in andriod studio crashes immediately with a blackscreen when I open it. The app uses an iframe embed url.Compiles fine but crashes with a black screen when I open it.
Why is it crashing?
public class MainActivity extends AppCompatActivity {
private WebView zwebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView = (WebView) findViewById(R.id.myWebView);
zwebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
I just copy-paste your code and its working fine the video is loading in the webview so if you face issue can you kindly share the log details that what error you actually getting.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView zwebView = new WebView(this);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
setContentView(zwebView);
}
}
Here is the code which I use and it's working. not crashing after all.

Android Studio - WebView - open internal links inside WebView, external in default browser

I am trying to add an WebView to my app, but would like to allow to open inside my app only links which are connected with my domain (www.example.com). All other links which doesn't contain the name of my domain (example.com) should be opened externaly by default browser. Using information from stockoverflow I have prepared the following code, but it still doesn't work and all links are opened inside my app, despite shouldOverrideUrlLoading is created ("if (Uri.parse(url).getHost().contains("example.com"))"). Is anyone able to help to resolve this problem? I was trying a lot of samples, but still cannot find the way how to fix it, but I fell that it can be just a small mistake in the code.
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Resolved! I am not sure that the code is 100% correct, but works fine and maybe it will be useful for someone (I am not the author, I just used a few samples from stackoverflow and put them together):
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("https://www.example.uk");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Android WebPage does not refresh its content

I have an android app with this code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setScrollbarFadingEnabled(false);
mWebView.loadUrl(url);
}
The WebView opens me the ABC.html page correctly. When I edit the ABC.html page and I open it again using my device, I don't see the new updates I made. It still loads the old page. I added this line:
mWebView.reload();
But the WebView loads the old page again. What can I do?
try:
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
and maybe also
mWebView.getSettings().setAppCacheEnabled(false);
although this last line should not matter.

Categories