Developed an Android application have included Webview along with progress bar and support for orientation change but my application crashes generating following error:
java.lang.IllegalArgumentException: View not attached to window manager
Here is my code:-
protected ProgressDialog mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
mSpinner = new ProgressDialog(this);
mSpinner.setMessage("Loading..Please Wait!");
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.com");}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
public class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Log.d("mytag","Page Loading Started");
mSpinner.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if ( mSpinner.isShowing()) {
mSpinner.dismiss();
//Log.d("mytag","Page Loading finished");
//mSpinner.dismiss();
}
}
Related
I am using android in that I am calling a link via webview. Now, the problem is that whenever I click any button or call any page of that link at that time after some second progress bar coming i.e. progress bar performance is delay while load anything.
From this problem user can't know that button/link has been clicked or not, without getting any progress bar sign, and hence from that user clicked many times that button/link.
Here is my android code of MainActivity.java,
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.progressBar2);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://florro-001-site2.etempurl.com/");
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I searched many things but related topics I didn't get.
use bar.setVisibility(View.VISIBLE); in the shouldOverrideUrlLoading not in onPageStarted try to use like that
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
bar.setVisibility(View.VISIBLE);
return true;
}
}
Android - Am loading a following url in webview "https://www.pinterest.com", it loads successfully, but when i click on login via facebook or login via google in pinterest it's loading a blank screen rather than loading the actual result, below is my code, please help me. The same code is working for other urls, but not for Pinterest alone.
public class Pinterest extends Fragment {
private WebView wv1;
String url="https://www.pinterest.com";
private ProgressBar progress;
View mView;
WebSettings settings;
ImageView back,top;
public Pinterest() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView= inflater.inflate(R.layout.pinterest, container, false);
wv1 = mView.findViewById(R.id.pin_webView);
progress = mView.findViewById(R.id.pin_progressBar);
back= mView.findViewById(R.id.go_back);
top= mView.findViewById(R.id.goToTop);
progress.setMax(100);
settings = wv1.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
wv1.setWebViewClient(new myWebClient());
wv1.setVerticalScrollBarEnabled(true);
wv1.setHorizontalScrollBarEnabled(true);
wv1.loadUrl(url);
progress.setProgress(0);
wv1.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
setValue(newProgress);
if (newProgress == 100) {
progress.setVisibility(View.GONE);
}
super.onProgressChanged(view, newProgress);
}
});
back.setOnClickListener(v -> {
if(wv1.canGoBack())wv1.goBack();
});
top.setOnClickListener(v -> {
Constants.goTop(wv1);
});
return mView;
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
System.out.println("when you click on any interlink on webview that
time you got url :-" + url);
// view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("your current url when webpage loading.. finish"
+ url);
super.onPageFinished(view, url);
}
}
}
It looks like requests from WebView are working only when the Facebook app is installed on the device.
See Practice #5 in the Facebook docs.
https://developers.facebook.com/docs/facebook-login/best-practices?locale=en_US
Google has been blocking since April 2017 all the requests related OAuth2, which are using WebView (due to security concerns).
I have a webview in my android application which renders html pages from a local folder, Now I want to show progress dialog when navigating from one html page to another but the progress dialog which I am using isn't showing up. Here is my code snippet:
#JavascriptInterface
public void save(String respString, boolean ifEndNode) throws JSONException {
ProgressDialog progDialog = null;
try {
if(ifEndNode){
//start loader
progDialog = new ProgressDialog(webView.getContext());
progDialog.setMessage("Saving survey, Please DON'T close the Application!! ");
progDialog.setCanceledOnTouchOutside(false);
progDialog.setCancelable(false);
progDialog.show();
}
//some code here
//...
}
catch (Exception e)
{
Logger.e(context,"exception", "jsonObjectexception");
}
finally {
if(progDialog != null && progDialog.isShowing())
progDialog.dismiss();
}
Can anyone suggest what the problem could be?
You can use WebView with ProgressDialog this way. This is nice and simple approch.
private WebView webView;
ProgressDialog prDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_news);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
webView = (WebView) findViewById(R.id.wv_news);
webView.setWebViewClient(new MyWebViewClient());
String url = "http://google.com/";
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.loadUrl(url);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = new ProgressDialog(NewsActivity.this);
prDialog.setMessage("Please wait ...");
prDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(prDialog!=null){
prDialog.dismiss();
}
}
}
I want to make a PDF viewer using a WebView and Google Docs url.
However, for some reason it isn't showed properly when it reaches 2nd page and successive, except on Android 5.
Here is an image from Android 4.1.1:
http://oi57.tinypic.com/6rrbpy.jpg
And here other from Android 5.0.0:
http://oi62.tinypic.com/2vdlefm.jpg
Any idea why this happens?
Here is my code
public class PDFViewer extends Activity {
public void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
this.setContentView(R.layout.web_viewer);
WebView myWebView = (WebView) this.findViewById(R.id.webViewSimple);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (progressDialog == null) {
progressDialog = new ProgressDialog(PDFViewer.this);
progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
});
String link=getIntent().getExtras().getString("link");
myWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+link);
}
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();
}
} }