I'm using an URL in webview, when I click a link it should direct it to a custom url like paynow://xxxxx.
When I use the webview client it is coming sometimes and not redirect sometime.
Is there any wrong in my code.
please check the url onPageStarted and shouldOverrideUrlLoading
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
MainApplication.getComponent(this).inject(this);
back=findViewById(R.id.back_w);
front=findViewById(R.id.right_w);
wv1=(WebView)findViewById(R.id.webview);
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setWebViewClient(new MyBrowser());
wv1.setWebChromeClient(new WebChromeClient());
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(Constants.Load_url);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return super.shouldOverrideUrlLoading(view, uri.toString());
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (url.startsWith("paynow://"))
{
if (url.contains("order"))
{
startActivityForResult(new Intent(context,TipsActivity.class).putExtra("url",url),1);
}
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
wv1.setVisibility(View.GONE);
}
}
Answer:
WebView is blocking popup which redirect to custom url
by adding wv1.getSettings().setPluginState(WebSettings.PluginState.ON); it worked
You need to move the logic in onPageStarted to the shouldOverrideUrlLoading functions.
WebView is blocking popup which redirect to custom url, by adding wv1.getSettings().setPluginState(WebSettings.PluginState.ON); it worked
Related
I have a web view and i have this code (P.S i am a beginer in programming)
private WebChromeClient getChromeClient() {
return new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressDialog.show();
if (newProgress ==100){
progressDialog.dismiss();
}
super.onProgressChanged(view, newProgress);
mWebView.setVisibility(View.GONE);
}
};
}
I already have webview client and webchrome client, you can see and download my code files here (cloud.mail.ru/public/2hHz/25DMkxh3U)
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mWebView.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
if (mbErrorOccured == false && mbReloadPressed) {
hideErrorLayout();
mbReloadPressed = false;
}
super.onPageFinished(view, url);
mWebView.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
At the top you can see my code in Main activity.
You can check how to find if the webview loaded completely at this link :
here
Once you start loading the webview, you can start a handler and check if the page is loaded completely, or you can reload the webpage.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// check if the webview is loaded completely after 30 seconds here
//if the page is not loaded then call the below line to reload
// mWebview.loadUrl("http://www.google.com");
}
}, 30000);
Override Webviewclient class and start a countdowntimer in onPageStarted method and cancel the timer in onPageFinished , invoke your method in onfinish of countdowntimer.
I am using a webview in my android app. Now the issue is in that webview it is opening like a desktop site not like a mobile platform site.
As for example, when I open the page in webview it is being shown like
And the same page when I am opening in web browser it looks like;
Now my question is I want to open the page in my web view like it is in web browser and I have no idea why it is behaving like that.
My webview code is like;
final WebView webview = (WebView)vi.findViewById(R.id.webview);
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
set.setJavaScriptEnabled(true);
set.setDomStorageEnabled(true);
set.setLoadWithOverviewMode(true);
set.setUseWideViewPort(true);
set.setSaveFormData(true);
set.setJavaScriptCanOpenWindowsAutomatically(true);
set.setBuiltInZoomControls(true);
set.setDisplayZoomControls(false);
set.setSupportZoom(false);
set.setDefaultTextEncodingName("utf-8");
WebChromeClient webClient = new WebChromeClient(){
public void onCloseWindow(Window w){
super.onCloseWindow(webview);
System.out.println("Closed");
}
};
webview.setWebChromeClient(webClient);
webview.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
webview.setInitialScale(1);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.loadUrl(url);
I changed the value of webview.setInitialScale(1); but no luck so far.
I can not change the HTML of the pages, so I need to do some kind of changes in webview end.
N.B. The attached images of page is same HTML page opening in browser and webview. PLEASE IGNORE THE BACKGROUND COLOR.
Any help will be very good for me. Thanks in advance.
This works for me
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
initViews();
}
protected void initViews() {
webView = (WebView) findViewById(R.id.webView);
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getCertificate();
webView.setWebViewClient(new SSLTolerantWebViewClient());
webView.loadUrl("https://www.google.com");
}
private class SSLTolerantWebViewClient extends WebViewClient {
// ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.d("ddd", "ddd");
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
handler.proceed();
}
}
}
Is your page responsive ? Try opening page in mobile browser. if it is not responsive make it responsive. Make sure you have added viewport meta tag in html like -
I have a Web View in Android,and I open a html web page in it.But it's full of links and images,and when I click one of them,it loads in my webview. So I want to disable this behavior,so if I click on a link,don't load it. I've tried this solution and edited a bit for myself, but not worked.
Please give me a solution with full code. I'm new to Java and Android.
My Full code here:
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
WebView webView;
String url="http://example.com";
TextView textView;
//to hide progressbar after loading part 1
LinearLayout liProgressContainer;
private String currentUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
textView = (TextView) findViewById(R.id.tvLoadingPercentage);
//to hide progressbar after loading part 2
liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
textView.setText(progress + " %");
}
});
webView.setWebViewClient(new MyWebViewClient());
WebSettings browserSetting = webView.getSettings();
browserSetting.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
//back button function
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
}
}
}
You need to configure your shouldOverrideUrlLoading method in your MyWebViewClient class to only load a URL should it match your desired URL. Add a field to your MyWebViewClient class to store a URL and add a single-arg constructor.
private String desiredUrl;
public MyWebViewClient(String url) {
this.desiredUrl = url;
}
In your shouldOverrideUrlLoading method, check to see if the supplied URL is equal to your URL instance variable.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (desiredUrl.equals(url))
view.loadUrl(url);
return true;
}
When setting your web view client, pass your URL argument, e.g.
webView.setWebViewClient(new MyWebViewClient(url));
I need to get a url when user press somethings in a webview.
I use this code:
webView = (WebView) rootView.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("my url");
webView.setWebViewClient(new MyWebClient());
MyWebClien
private class MyWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("URL", url);
return true;
}
}
But, if i click something, the event is never called.
Thank you
shouldOverrideUrlLoading(WebView view, String url) will get called on clicking a link on a webview.
usage example:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
// do whatever you want to do on a web link click
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
webView.loadUrl("url to open first webpage on a webview");
Based on this: how to get the current page url from the web view in android
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
System.out.println("your current url when webpage loading.." + url);
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("your current url when webpage loading.. finish" + url);
super.onPageFinished(view, url);
}
#Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("when you click on any interlink on webview that time you got url :-" + url);
return super.shouldOverrideUrlLoading(view, url);
}
});
I would like to be notify each time before and after the WebViewClient loads a resource.
I found onLoadResource and it seams to be before load.
Is there any possibility to be notified after the resource is loaded?
Try the below code-
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
//Do something
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//Do something
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
//Do something
super.onPageFinished(view, url);
}
}
It may help you.