Android application is stuck on links in Android Webview - android

Background
I am making an app which uses Quora links All Links(profiles,answers,topics,except sign in) are Opening in My App only .
Problem
There is a option "Open in App" , I want to Remove that
Option.Clicking on the Cross Button Doesn't work.
User Can't Sign in the Quora , Sign in Link is not opening.
In both cases the app is stuck .I tried WebView Client Solution that is
working fine but for sign in case it is not working.

There are two require that you need:
Hide element of website (here is "Open in App" button)
Disable event of element in website (here is signin click)
and solution you can found at WebViewClient class:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(!url.equals(currentUrl)){ //currentUrl is url that signin with navigated.
view.loadUrl(url);
}
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// hide element by class name
webview.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('your_class_name')[0].style.display='none'; })()");
// hide element by id
webview.loadUrl("javascript:(function() { " +
"document.getElementById('your_id')[0].style.display='none';})()");
}
});

Related

How to intercept Android App Link into a WebView?

I'm trying to implement the following behavior into my Android App.
The app is a native app with some parts using WebView, in this WebView there is the possibility of using different payment methods.
If the user has the app of that bank installed I want to be able to open the corresponding app. In some cases, the website loaded into the WebView launch a specific intent:// that I can easily intercept and try to redirect the user to the app, but for the cases where the website use the new Android App link I'm not able to intercept them because they are normal https:// calls.
I tried to load the Android App link into Chrome and this way the app is opened.
My question at this point is how I can replicate the behavior of Chrome into my WebView??
I didn't find that much informations on this specific case besides the Android Documentation
Thanks for your help :)
You can intercept the url click by using custom WebViewClient for the webview.
1)Set the custom webview client for the webview
2)Override the below method and return true (you can do this based on particular url also. )
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
3)Override below method and handle the click and start the activity using either package name or playstore url with intent.
public void onLoadResource (WebView view,
String url)
WebView mWebView;
WebViewClient customWebClient = new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
//This can be modified based on the url . URL can be retrieved using request.getUrl()
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("") ){
// launch playstore activity
final String appPackageName = "package name of the application"
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
}
mWebView.setWebViewClient(customWebClient);

Detecting which link opened in chrome custom tab

In webView we can detect user opend which link like following code:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
);
but in custom chrome tab i can't find a way to detect which link opend?
is it possible have control on internal link like webView in CustomTab?
thanks.
It's not possible to track the user navigation that's happening inside a Custom Tab from the host application.

Denied starting an intent without a user gesture Webview Android

Trying to redirect local html page in android webview using Javascript redirect, gets denied starting an intent in Logcat:
Testing on android 5.1.1
document.location = "index.html";
Denied starting an intent without a user gesture, URI:
file:///android_asset/index.html
I read the documentation in 1,000 attempts Android.developer and this was my solution
I do not know if you understand, I speak Spanish
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
This worked for me:
webView.setWebViewClient(new WebViewClient());
There are few issues here.
From newest androids, the WebView and Chrome Client is separated application which can be automatically updated without user intention.
From Chrome x >= 25 version, they changed how loading url is working in android application which is using webview component. https://developer.chrome.com/multidevice/android/intents Looks like they are blocking changing url without user gesture and launched from JavaScript timers
Solution here is to force user to activate URL change, for example on button click.
Also, you can override method mentioned above "shouldOverrideUrlLoading" in WebView client.
As alternate, i figured out was to add addJavascriptInterface each button click event fire action to JavascriptInterface
webView.addJavascriptInterface(new java2JSAgent(), "java2JSAgentVar"); //webView webview object
public class java2JSAgent
{
#JavascriptInterface
public String getContacts()
{
String jsonResponse = "{result:'redirected'}";
runOnUiThread(new Runnable() {
#Override
public void run() {
webView.loadUrl("file:///android_asset/index.html");
}
});
return jsonResponse;
}
}
might not be a good approach but atleast its working :-)
Thanks

WebView Changes the Url Automatically

I am very new in App Development with Eclipse for android. I am able to make the buttons and links but am stuck at few places, and need help here.
The App has 3 button and a webview .
I am loading my Gmail account in webview by clicking the button . these buttonos are shortcuts like Inbox and Junk Box.So i should be able to check the mail by just clicking the inbox button.
The problems I face are:
When i click the inbox button for first time its ask for username and password but when i click login- It changes the webview Url to something else and ends with not found!! or error .I want to stop the Url from changing.
Webview Opens a new default webbrowser of my phone with the address bar on the top.
I want to hide that address bar and dont want to use the default browser to open the Page.
It has to be a smoth transaction without jumoing to URl page.
Hi Thank you for the response but i have few errors here,1) Since i am using three button , to activate the webview i have used the switch view ,therefore i am not able to define the above below is the sample code:
cancelbutton.setOnClickListener(new View.OnClickListener() {
private Activity result;
#SuppressWarnings("deprecation")
public void onClick(View v)
{
{ switch (v.getId())
{
case R.id.can_button3:
Intent myintent1 = new Intent
(Launcher.this,cancelbutton.class); Toast.makeText(Launcher.this, "Cancellation ",
Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
WebView = (WebView) findViewById(R.id.webView1);
WebView.getSettings().setJavaScriptEnabled(true);
WebView.getSettings().setSavePassword(true);
WebView.loadUrl("https://gmail.com/inbox");break;
}
} }
}
);
junkBox.setOnClickListener(new View.OnClickListener() //Next Button{private Activity
result;
Create a CustomWebViewClient for your WebView. Override the following two functions:
shouldOverrideUrlLoading - To display the page within WebView
onReceivedSslError - To ignore SSL errors.
The code would look like this:
public class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
Add this WebViewClient to your WebView:
WebView webView = (WebView) result.findViewById(R.id.web_view);
webView.setWebViewClient(new CustomWebViewClient());

How To Link To An Android App From Android Webview

Currently I have an Android app that is basically a web view loading a web page. On the web page I've tryed to link to the market like this...
https://market.android.com/details?id=com.google.earth
http://market.android.com/details?id=com.google.earth
market://details?id=com.google.earth
The first result just opens up a white screen (It may be loading, but it has been their for over a minute).
The second result says that the page has been moved and a link. If you click th link it does what the first one did.
The third result says that the page may be temporarily down. (It's treating the link like its online rather than in the phone itself)
Here is how the link looks...
echo "<a href='https://market.android.com/details?id=com.google.earth' data-role='button'>Upgrade Now</a>";
Remember the web page I'm loading is using JQuery Mobile and I'm echoing the link with php.
How can I open a link to the Android Market in a webview on a web page?
In my case, I was getting the same blank/moved temporarily trouble described. I wanted to use the shouldOverrideUrlLoading so that the native browser wasn't used in an oauth2 flow from my page to google and back to my page. My android app was talking to localhost/tomcat with a self-signed cert. Turned out I needed to call proceed because of cert mismatch:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("DevAgentSocialMain", "URL: " + url);
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("DevAgentSocialMain", "onReceivedError: " + errorCode + " " + description + " url: " + failingUrl);
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.i("DevAgentSocialMain", "onReceivedSslError: " + error.getPrimaryError());
//super.onReceivedSslError(view, handler, error);
handler.proceed();
}
});
You can use a callback when a user clicks a link inside the webview.
See
Handling Page Navigationsection on the android developer platform.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);
Then your callback
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Categories

Resources