The app does a login to a web application using WebView. Once in the webview, the webview appears to handle everything for you as it should based on the user's clicks. However I need to review on each event if the URL changes to a specific logout URL.
How can I return the user to the app itself when the user logs out on the web application within the webview? I do not want the webview to stay as the active view.
I have tried WebViewClient.shouldOverrideUrlLoading and View.OnTouchListsner.
The class I tried to implement public but it didn't allow me.
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
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
Here's the code before I want to call the method to check the URL;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText phone = (EditText) findViewById(R.id.phone1);
final EditText login = (EditText) findViewById(R.id.uname);
final EditText pass = (EditText) findViewById(R.id.password);
phone.requestFocus();
final Context context = this;
submit = (Button)findViewById(R.id.submit);
sbutton = (Button)findViewById(R.id.loginCred);
chcred = (CheckBox) findViewById(R.id.cBox);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textstring(phone, login, pass);
encPhone = URLEncoder.encode(Phone);
encLogin = URLEncoder.encode(Login);
encPass = URLEncoder.encode(Pass);
if (chcred.isChecked()) {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
prefwrite(editor);
}
pushurl();
clear(phone, login, pass);
}
});
WebViewClient() //Right here is where I want to call it
This can help you:
https://developer.android.com/reference/android/webkit/WebViewClient.html#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)
The event does not notify you when the url changes, but does when the page starts to load, at that point you only need to verify the current url.
Try This
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
Uri uri = Uri.parse(url);
if (uri.getScheme().contains("google") || uri.getScheme().contains("tel")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
if (intent.resolveActivity(getPackageManager()) != null)
startActivity(intent);
return true;
} catch (URISyntaxException use) {
Log.e("TAG", use.getMessage());
}
} else {
webview.loadUrl(url);
Log.e("url", url);
}
return true;
}
#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);
}
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
super.doUpdateVisitedHistory(view, url, isReload);
String currentUrl=view.getUrl();
}
});
webView.loadUrl(url);
here webView is my WebView and url is the url which I am using in my webview.
If you want to make some changes with the change in url,then go for
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
super.doUpdateVisitedHistory(view, url, isReload);
String currentUrl=view.getUrl();
}
you will get the url updation in this call.
Related
I want to hide header and footer of the website but its not working Please Help
I wanted to know if it was possible to display only certain parts of a website in a WebView.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://myopenhab.org/account");
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:(function() { " +
"var head = document.getElementId('mainHeader').style.display='none'; " +
"})()");
}
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
I have tried this Javascript Code but its showing website then after its removing
public class myWebClient extends WebViewClient
{
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:(function() { " +
"var element = document.getElementById('mainHeader');"
+ "element.parentNode.removeChild(element);" +
"var element = document.getElementById('footerRights');"+ "element.parentNode.removeChild(element);" + "})()");
}
#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);
}
}
When I attempt to open the Stripe Checkout inside my webview, I receive an error saying "there was a problem loading Checkout. If this persists, please try a different browser". When I run through the checkout process on Chrome Mobile outside of the Webview, the stripe checkout works flawlessly. It redirects to a webpage for the stripe checkout. Do I need to enable something in the application to allow for this to work in the WebView?
Code:
public class login extends Activity {
private WebView mWebView;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mWebView = (WebView) findViewById(R.id.activity_login_webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBar.setVisibility(View.VISIBLE);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.loadUrl("http://www.icadeliveries.com/login");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient 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 webView, String url) {
webView.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
TextView load = (TextView) findViewById(R.id.LoadingText);
load.setVisibility(view.GONE);
progressBar.setVisibility(view.GONE);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) { //if back key is pressed
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
Unfortunately, Stripe doesn't support displaying Checkout in Webviews at the moment which is why you're getting this error. The best solution is to build your own payment form using Stripe.js
i have a problem. i am using WebView Object in my project. in MainActivity.java code side i use webbBrowser.setWebViewClient(new ViewBrowser());
there is second class having name ViewBrowser.java this file is extended with WebViewClient.
by using this Client service in this java file there are 2 functions Overrode
1 - shouldOverrideUrlLoading(WebView view, String url)
2 - onPageFinished(WebView view, String url)
in onPageFinished() i have used view.getTitle(); i want to set this title to TextView on my MainActivity class. can any body help me for this.
this is my MainActivity.java class:
public class MainActivity extends Activity implements OnClickListener {
Button bttnSearch;
EditText txttInput;
TextView txttView;
WebView webbBrowser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
bttnSearch = (Button) findViewById(R.id.btnGo);
txttInput = (EditText) findViewById(R.id.txtInput);
txttView = (TextView) findViewById(R.id.txtView);
webbBrowser = (WebView) findViewById(R.id.broWebView);
webbBrowser.setWebViewClient(new ViewBrowser());
webbBrowser.getSettings().setLoadsImagesAutomatically(true);
webbBrowser.getSettings().setJavaScriptEnabled(true);
webbBrowser.getSettings().setLoadWithOverviewMode(true);
webbBrowser.getSettings().setUseWideViewPort(true);
webbBrowser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webbBrowser.setScrollbarFadingEnabled(true);
webbBrowser.loadUrl("http:/www.google.com");
bttnSearch.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String urlText;
urlText = txttInput.getText().toString();
switch(v.getId()){
case R.id.btnGo:
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
webbBrowser.loadUrl(urlText);
break;
}
} }
this is my ViewBrowser.java:
public class QaziViewBrowser extends WebViewClient {
//private Toast txttView;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// i want to get Title text of the web page and set to txttView
}
}
in you activity on create register the broadcast like this
registerReceiver(updateText, new IntentFilter("UPDATE_TEXT"));
onDestroy unregister
unregisterReceiver(updateText);
make a broadcast reciever
BroadcastReceiver updateText = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive()");
// check if intent is not null and other verification if needed
TextView.setText(intent.getStringExtra("title"));
}
};
in your webview class what u have to do is fire boradcast putting the title in intent
Intent intentToBroadcast = new Intent("UPDATE_TEXT");
intentToBroadcast.putExtra(title, webview.getTitle());
mContext.sendBroadcast(intentToBroadcast);
i haven't test run this code but this is the logic that will work, hope it helps.
Its quite simple.
In the callback onPageFinished you can get the title of the page loaded.
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// i want to get Title text of the web page and set to txttView
String title = view.getTitle();
// Use title where ever and which ever way you want.
}
You can use Jsoup library(Download link : http://jsoup.org/).
and onPageFinished() method use below code
onPageFinished(WebView view, String url){
new ContentDownloader(url).execute();
}
private class ContentDownloader extends AsyncTask<Void, Void, Void>
{
Document document;
String url;
public ContentDownloader(String p_url)
{
url = p_url;
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
m_progressDialog = ProgressDialog.show(MainActivity.this, "Wait", "Content Downloading..");
}
#Override
protected Void doInBackground(Void... params)
{
try
{
document = Jsoup.connect(url).post();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
if(document != null) {
m_tvTitle.setText(m_document.title());
}
m_progressDialog.dismiss();
}
On Android, I have a webView. I'm loading a url on it. Firstly, I get a login page of Facebook and after filling login details it gives me a JSON Response on same webView.
I'm using this code to load that URL but now I don't know how to get that JSON Response in a string what I got on finished of that url.
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
wb.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
};
wb = (WebView) findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setSupportZoom(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.setWebViewClient(yourWebClient);
wb.loadUrl("http://pqrs.abcde.com/facebook");
The format of JSON Response is:
{"status":"success","msg":"You have successfully logged in."}
Build a class called MyJavaScriptInterface. And MyJavaScriptInterface to WebView.
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
wb.loadUrl("javascript:HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
};
wb = (WebView) findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setSupportZoom(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.setWebViewClient(yourWebClient);
wb.loadUrl("http://pqrs.abcde.com/facebook");
wb.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
#JavascriptInterface
public void showHTML(String html) {
System.out.println(html);
}
}
Note: From you are required to remove window in wb.loadUrl("javascript:...");.
And #JavascriptInterface before "public void showHTML".
You can get content from showHtml(..) in html parameter.
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(webdata);
}
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
view.loadUrl(url);
return true;
}
}
What do I need to my code to make the dialog dismiss() after the webview is loaded?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new homeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.loadUrl("http://google.com");
ProgressDialog pd = ProgressDialog.show(Home.this, "",
"Loading. Please wait...", true);
}
I've tried
public void onPageFinshed(WebView view, String url){
pd.dismiss();
}
Didn't work.
:o
webview.setWebViewClient(new homeClient()); homeClient()????
try this
...
...
...
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
webview.loadUrl("http://www.google.com");
}
Update::
this is a good example.
Android WebView and the Indeterminant Progress Solution
This is OK.
public class WordActivity extends Activity {
private WebView webview;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Bundle objetbunble = this.getIntent().getExtras();
webview = (WebView) findViewById(R.id.webview);
final Activity activity = this;
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Chargement en cours");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
});
webview.loadUrl("http://www.example.com");
}
}
#Jorgesys this is not 100% accurate. If you have several iframes in a page you will have multiple onPageFinished (and onPageStarted). And if you have several redirects it may also fail. This approach i think solves all the problems:
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
} else{
redirect = false;
}
}
});
How are you accessing pd in onPageFinshed()? (And are you sure it's actually called when the page loads?)
In your onCreate(), try passing pd to your homeClient so that homeClient can take care of dismissing the dialog.
Your homeClient should look like this:
private class homeClient extends WebViewClient {
private ProgressDialog pd;
public homeClient(ProgressDialog pd) {
this.pd = pd;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished (WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}
}
}
In your onCreate():
ProgressDialog pd = ProgressDialog.show(Fbook.this, "",
"Loading. Please wait...", true);
webview.setWebViewClient(new homeClient(pd));
If you simply want to show Progress before webPage loading in WebView. You can simply request for Window Feature Progress like
getWindow().requestFeature(Window.FEATURE_PROGRESS);
before setContentView(R.layout.blahblah);
and show it progress in onProgressChanged like
final Activity context= this;
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView webView, int progress)
{
activity.setProgress(progress * 1000);
}
});
And if you want to add you own ProgressDialog then use WebviewClient
webView.setWebViewClient(new WebViewClient() {
ProgressDialog rogressDialog ;
#Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
progressDialog = ProgressDialog.show(context, "Loading...", "Please wait...");//where context = YourActivity.this;
super.onPageStarted(view, url, bitmap);
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog .dismiss();
super.onPageFinished(view, url);
}
});
webView.loadUrl(url);
wv1.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(Entertainment.this, description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
String webUrl = wv1.getUrl();
}
});
wv1.loadUrl(url);
The above code will show a progress dialogue for every incoming link you navigate to the in webview.