In my app, I load a WebView where users enter username/password and I don't want the webview to close should the users accidentally click outside of the webview since the page will have to make another network call to load this webview.
I know this can be done for Dialog (like so: dialog.setCanceledOnTouchOutside(true);) but couldn't find similar mechanism for webview.
Thanks!
Here is the code:
#SuppressLint("SetJavaScriptEnabled")
public class LoginDialogFragment extends DialogFragment {
private static final String TAG = LoginDialogFragment.class.getSimpleName();
View mainView = null;
WebView webView = null;
OnLoginSucceeded loginSucceeded = null;
ProgressBar progressBar = null;
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.webview, container, false);
if (getDialog() != null)
getDialog().setTitle(getString(R.string.login));
webView = (WebView) mainView.findViewById(R.id.webview);
//display the progress bar
progressBar = (ProgressBar) mainView.findViewById(R.id.user_profile_loading_progress);
progressBar.setVisibility(View.VISIBLE);
//turn caching off
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
});
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i(TAG, "URL received in onCreateView's onPageStarted is: " + url);
new HandleRequest().execute(url);
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
new FetchLoginFormTask().execute();
return mainView;
}
I found my answer here and I just used getDialog().setCanceledOnTouchOutside(false); ensuring that when back key is clicked, the webview is dismissed.
Try with override the method:
#Override
public void setCancelable(boolean cancelable) {
super.setCancelable(false);
}
If your issue is not getting solved by this then please put some code so i can help you.
Feel free for comment.
Related
progress bar will not stop and will load indefinitely.
Referred Page:
progress bar in android webview
fragment code
public class testFragment extends Fragment {
private WebView mWebView;
private WebSettings mWebSettings;
private ProgressBar progressBar;
public testFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#SuppressLint("SetJavaScriptEnabled")
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testfragment, container, false);
mWebView = (WebView) v.findViewById(R.id.webView);
progressBar = (ProgressBar) v.findViewById(R.id.pro);
//progressBar.setVisibility(View.GONE);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.setWebViewClient(new WebViewClient());
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setSupportMultipleWindows(false);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(false);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportZoom(false);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebSettings.setDomStorageEnabled(true);
mWebView.loadUrl("https://stackoverflow.com/");
return v;
}
}
Removing the annotation from the annotation will completely disappear the progress bar.
I also wanted to use #Override public void onProgressChanged (WebView view, int newProgress) on the page referenced, but it did not appear in the available methods.
The issue is,
you have set 2 web clients and the last statement was overriding the first one. So visible & hide was never executed.
Since in UI, you have set visibility for progress bar as visible .So it is always visible.
Remove mWebView.setWebViewClient(new WebViewClient()); line.
For onProgressChanged()
You have to use WebChromeClient. Since is available in Chrome Client, not on WebViewClient
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
});
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).
This code is working fine in Activity but same code doesn't work in Fragments. It gets error of ProgressDialog which can't be applied and another one is can't resolve method onBackPressed()
public class FacebookFragment extends Fragment {
private WebView webView;
public FacebookFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView= inflater.inflate(R.layout.fragment_facebook, container, false);
webView = (WebView)rootView. findViewById(R.id.webView1);
startWebView("https://betanews.in/");
return rootView;
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(FacebookFragment.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
// Open previous opened link from history on webview when back button pressed
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
replace this line progressDialog = new ProgressDialog(FacebookFragment.this); with progressDialog = new ProgressDialog(getActivity());
we need to pass a context to the ProgressDialog constructor, FacebookFragment.this will return object of Fragment, but the expected object is of Activity, you can get the Context of holding Activity.
onBackPressed() is method from class Activity , but here you are extending Fragment
override this method where you actually inflates this fragment. and check for the instance id of the fragment you needed, then do the stuff.
hope it help you
I am experimenting with the loopj package. I am trying to make a HTTP request to a website and display the website in the webview.
I am successfully getting a result back, however the web view does not display the page as desired, instead chrome opens up and displays the page.
Am I missing something or is there a way I can override this unwanted behaviour?
Below is my oncreate method where I am making the request:
public class MainActivity extends Activity {
Button connectBtn;
TextView status;
WebView display;
String url = "http://www.google.com";
AsyncHttpClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.statusbox);
connectBtn = (Button)findViewById(R.id.connectBtn);
display = (WebView)findViewById(R.id.webView1);
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(String response) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
display.loadUrl(url);
}
});
}
});
}
setWebViewClient to your WebView and override shouldOverrideUrlLoading() now write view.loadUrl(url); in that method.
Just add this code,
display.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
You need to set a WebViewClient and override the shouldOverrideUrlLoading method. Something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
view.loadUrl(url);
}
});
That makes sure that clicks on links in the WebView are handled by the WebView itself.
Edit: Actually, I misread the question. You aren't dealing with a click in the WebView itself, so this isn't relevant. Sorry!
just use this code under you webviewclient
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
final EditText editText = (EditText) findViewById(R.id.urlfield);
editText.setText(url);
}
}
Now im trying to show a page in webview, it takes some time to load, meanwhile i want to show a progressDialog. The best i could do was the code below, but the problem is that im doing it in onCreateView, so the progressDiealog only shows until the view is created, which is not what i want. I know that i have to use it in onActivityCreated , but im not sure how to do that , thanks all in advance
public class someFragment extends SherlockFragment {
Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
View v = inflater.inflate(R.layout.some_layout, group, false);
final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Loading", true);
final WebView myWebView = (WebView) v.findViewById(R.id.webViewsome);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(false);
myWebView.getSettings().setSupportZoom(false);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.setInitialScale(25);
myWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
myWebView.loadUrl("http://www.test.com/test-0.jpg");
return v;
}
}
Try this in SomeFragment:
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
//remove your progressdialog here
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//show your progressdialog here
super.onPageStarted(view, url, favicon);
}
}
then:
webView.setWebViewClient(new MyWebViewClient());
One thing you can do is, in onCreateView start a AsyncTask with a progressDialog in it. In onPageFinished(after web page completely loaded) of webView, cancel that task. Hope its useful.