I am trying to load my site in android web view, whenever I set url to google.com or any other it loads without any problem but when I try to load my site darpankulkarni.in then it just shows blank screen.
WebViewActivity:
public class WebViewActivity extends Activity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webView = (WebView) findViewById(R.id.webView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.darpankulkarni.in");
//webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new MyWebViewClient());
}
}
MyWebViewClient:
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//webProg.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
fixed it by adding code below settings to the web view inside of onCreate
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
websettings.setAllowFileAccess(true);
websettings.setAllowContentAccess(true);
websettings.setAllowFileAccessFromFileURLs(true);
websettings.setAllowUniversalAccessFromFileURLs(true);
}
Apply the basic settings to your web view for loading the web view in view port
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
So Here is the complete working code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView=(WebView)findViewById(R.id.webView1);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
progressDialog.setMessage("Loading the Web View");
progressDialog.show();
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.darpankulkarni.in");
// mWebView.loadUrl("http://www.google.com");
// mWebView.setWebViewClient(new MyWebViewClient());
}
Don't forget to give the internet permission in your manifest
<uses-permission android:name="android.permission.INTERNET" />
I created a progress dialog for loading the web view as it taking some time to load.
Hope this vll help !!
Related
I m trying to load one of my cloud URL from android webview but it returns HTTP Status 401 always. If i tried from Chrome mobile browser its working as expected. Even in iOS WebView its working fine.
Here is my code, Please someone help me.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xoi);
base_url = "https://vision.xoeye.io/partners/abcd/share?";
String params = "shareId=12345678910111213141516" +
"&serviceTicket=123-123&" +
"customer=Manikanta&" +
"location=3201 S. State St. Chicago IL 60616";
webView = findViewById(R.id.webView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setDomStorageEnabled(true);
webView.clearHistory();
webView.clearCache(false);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.clearFormData();
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
webView.loadUrl(base_url);
}
Try This
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xoi);
base_url = base_url+params;
webView = findViewById(R.id.webView);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new HelloWebViewClient());
//add here additional setting which you want
webView.loadUrl(base_url);
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
Hope this will help you.
I'm opening a WebView with static html in my Android app. The html contains a link as well which currently gets opened in the system browser, i.e. the app is put in the background. How can I achieve opening that link in the WebView itself? I tried it with
webView.setWebViewClient(new WebViewclient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// gets never called
}
}
but that method is never called.
Try this it works for you.
private void loadWebView() {
webView = (WebView) view.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Loading...", true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://yourWeb.com/");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
try {
pd.dismiss();
} catch (Exception e) {
}
}
});
}
I am loading a pdf in WebView.
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient());
String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");
webView.loadUrl(url);
setContentView(webView);
Whenever the activity is started, WebView is shown blank. But I want to show a progress dialog till the WebView starts rendering pdf file. How can I add a listener to the WebView indicating that the files has been started visible to the user.
public class FGWebViewClient extends WebViewClient {
private WebViewClient wvc;
private static final String TAG=FGWebViewClient.class.getCanonicalName();
ProgressDialog mProgressDialog;
// when you add the webviewclient then show the progressDialog.
public FGWebViewClient(Context context) {
Log.i(TAG, "FGWebViewClient Called...");
this.wvc=wvc;
mProgressDialog=new ProgressDialog(context);
mProgressDialog.setMessage("URL Loading...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCanceledOnTouchOutside(false);
if(mProgressDialog!=null){
mProgressDialog.show();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG,"shouldOverrideUrlLoading");
return wvc.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i(TAG,"onPageStarted");
wvc.onPageStarted(view, url, favicon);
}
// OnpageFinished just dismiss the progressDialog.
#Override
public void onPageFinished(WebView view, String url) {
if(mProgressDialog!=null&&mProgressDialog.isShowing()){
mProgressDialog.dismiss();
mProgressDialog=null;
}
Log.i(TAG,"onPageFinished");
//To clear cache of webview
//view.clearCache(true);
//clearEveryThing(view);
wvc.onPageFinished(view,url);
}
and call like this
WebView webView=(WebView)v.findViewById(R.id.webView1);
FGWebViewClient webViewClient= new FGWebViewClient(context);
webView.setWebViewClient(webViewClient);
webView.setWebChromeClient(new WebChromeClient());
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl((webUrl).toString());
You can use setWebViewClient and display showProgress as below code
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginState(WebSettings.PluginState.ON);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
showProgress();
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
hideProgress();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
hideProgress();
}
});
String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");
webView.loadUrl(url);
You could create custom class and extend it with WebViewClient. Then override method onPageStarted where you could consider that it started rendering. For e.g :
private class CustomWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
showProgress();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dismissDialog();
}
}
guys! I have problem to load html page with android-webview. I need to load the url with my webview but not with the mobile system broswer or other broswer, so I have to apply the method setWebViewClient() to my webview but not WebChromeClient(). However, there's load nothing but blank page when applied the setWebViewClient(), and works fine with the later method. I don't know where's problem, here is the code:
.xml :
<WebView android:id="#+id/webview"
android:layout_marginTop="50dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
.java :
WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= 19) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
private String loadUrl = "http://www.baidu.com";
webView.loadUrl(loadUrl);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
view.loadUrl(url);
super.onLoadResource(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.loadUrl(url);
super.onPageStarted(view, url, favicon);
}
});
/*
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v(Log_Tag, String.valueOf(newProgress));
}
});
*/
Just put this code in your activity
private String loadUrl = "https://www.google.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.webView);
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
WebViewClient mWebViewClient = new WebViewClient();
mWebView.setWebViewClient(mWebViewClient);
mWebView.loadUrl(loadUrl);
}
You shouldn't override methods in WebViewClient class if you don't want to change their behavior or add some functionality. And don't forget to add permission Internet to your manifest file. WebView will not work without it.
Thanks #Mike M. again. The method shouldOverrideUrlLoading() should return false if you want to loading the url with your webView but not with the mobile system default browser or other Third-Party browsers. And, if you want to deal some javascript actions of the webpage with your webView, you're suggested to apply the WebChromClient to your webView.
Here is the good example:
WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load the page with cache
if (Build.VERSION.SDK_INT >= 19) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return true load with system-default-browser or other browsers, false with your webView
return false;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v(Log_Tag, String.valueOf(newProgress));
//put your code here if your want to show the progress with progressbar
}
});
private String loadUrl = "http://www.baidu.com";
webView.loadUrl(loadUrl);
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.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.w3schools.com/");
emphasized text
i want to play a video from youtube on webview.. it displays the video,But i want to play it on the same page i mean i've to use WebViewClient.. but using that it doesn't play the video.. (on pressing play button it doesn't play the video) what should i do? my code is
setContentView(R.layout.main);
wvSpecials = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wvSpecials.getSettings();
webSettings.setJavaScriptEnabled(true);
wvSpecials.loadUrl("http://here.com/is link/");
wvSpecials.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(
specialsActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.setMessage("Please wait...");
progressDialog.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
}
You may use the below listed code:
public class YouTube extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView;
myWebView = (WebView) findViewById( R.id.web);
myWebView.setWebViewClient(new MyWebViewClient());
String pre="<iframe class=youtube-player type=text/html width=";
String height=" height=";
String suffix=" src=http://www.youtube.com/embed/**xxxxxxxxxxx**?autoplay=1 frameborder=0>"; // replace xxxxxxxxxxx with the specific embed id of your video
String playVideo=pre+260+height+150+suffix;
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadData(playVideo,"text/html","UTF-8");
}
// override default behaviour of the browser
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "",
"Loading. Please wait...", true);
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
} }