We have a requirement where we want multiple webview instance to be open from same application.
E.g Android app1 should open page1.html in first webview instance and page2.html in second webview instance. When page2.html is started page1.html webview should go in background.
Is this possible in Android? Is yes, Can you please provide sample code or link which talks in details as how this requirement can be achieved.
public void callwebtwo(){
loadweb = (WebView) findViewById(R.id.loadweb);
loadweb2 = (WebView) findViewById(R.id.loadweb2);
loadweb2.getSettings().setJavaScriptEnabled(true);
loadweb.getSettings().setJavaScriptEnabled(true);
loadweb2.loadUrl("http://www.google.co.in/");
loadweb.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("shouldOverrideUrlLoading", url.toString());
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url.toString());
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url.toString());
super.onPageStarted(view, url, favicon);
}
}
});
loadweb2.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("shouldOverrideUrlLoading", url.toString());
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url.toString());
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url.toString());
super.onPageStarted(view, url, favicon);
}
});
}
instance of the both web is different so it will work and perform seperatly..
i gvn ans whtever i cn understand frm ur quest.
Yes you can do it in android.
Create seperate activities for both and when you load page2 in second webview instance then obvisouly page1 webview instance will be in back of second and when you press back from page2 then page1 will be infront.
Related
I can't open mht files in WebView.
It worked fine until Chrome v76, but v77 had a read error
If chrome is returned to v76, the read was successful.
file: /// also didn't work.
Loading with loadDataWithBaseURL also didn't work
webview.loadUrl("file://{$context.cacheDir}/file.mht")
cid: Frame- "Random string" # mhtml.blink
Net :: ERR_UNKNOWN_URL_SHCEME
Sorry for my late answer because i face same problem and i was searching solution yesterday and i resolve it today like below.
In your java class you have to give below code
webView.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
if (one_time==1)//one_time is only a variable because i want
// to run view.loadUrl(url); only one time.
view.loadUrl(url);
one_time++;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, final String url) {
}
});
I think you are giving
view.loadUrl(url);
in below method
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// view.loadUrl(url); like this
// you will need this when you will load the url but in case
//of mht page it will not work
return true;
}
do not do this if you are loading mht page because
shouldOverrideUrlLoading(WebView view, String url){...}
loads the url and it also run when first time you open webview activity.
so,
onLoadResource(WebView view, String url){...}
method load the Resource.
I have a PHP service that returns me HTML that I display in a WebView using
activity_news_complete_webview.loadDataWithBaseURL("", html, "text/html", "UTF-8", null);
Also implemented the WebViewClient as follows
WebViewClient webViewClient = new WebViewClient() {
#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);
}
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
};
In doing so when i tap on any , the WebViewClient shows that the url attribute is about:blank but the tag contain proper urla in html variable which i load in the WebView.
Any one who can suggest me a proper solution for this would be a great help, I have read many threads on this issue but haven't found any proper solution because anything i try gives the same issue.
You can use this line replace to .loadDataWithBaseURL();
activity_news_complete_webview.loadData(html, "text/html; charset=utf-8", "UTF-8");
Its working perfectly for me. try this one.
here my code...am already implemented WebViewClient concept.it works well in web view..In my App have content saring via fb,twitter,g+,,,fb also login and open in webview...but when am click share to fb icon in my app after that all process will work on browser..but i want all process in webview only...
oncreate
webView = (WebView) findViewById(R.id.webView1);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
if (Build.VERSION.SDK_INT < 8) {
...
} else {
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
webView.loadUrl("www.example.com.");
WebViewClient
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onLoadResource (WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/myerrorpage.html");
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
You must override your shouldOverrideUrlLoading() method in WebViewClient class and following code
#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;
}
Go through the documentation for more information.
Use a if clause in your shouldOverrideUrlLoading method that returns false;
Example from Building web apps in web views
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;
}
Using webview.loadUrl(url) method I open an url. If I click any Button on the view it directs to another page. Now I want to get the url of the directed page. how can I get it?
I also want the content that is displayed on the webview. How to get the content of the WebView ?
String webUrl = webView.getUrl();
please see my answer
may it will help you...!!!!
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("WebView", "your current url when webpage loading.." + url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WebView", "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 am assuming that you have set your WebViewClient.If not then you can do like below,
webView.setWebViewClient(new MyWebViewClient());
String currentUrl;
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
currentUrl=url;
return true;
}
}
Here when you click on any link on the WebView then it will call shouldOverrideUrlLoading() and you can get the current url there in currentUrl.
this is the most simple way of handling this for API > 20: There are obviously other methods with parsers like HTMLCleaner to have more control. However, this is good and simple for getting URL. Each time URL changes in Webview, URL in the request object changes as well.
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String temp = request.getUrl().toString();
if(temp.isEmpty()){
Log.i(ActivityName, "No url returned!");
}else{
Log.i(ActivityName, temp);
}
return false;
}
you can also make your call by overriding onpagefinished method of webviewclient. I wanted to note this, since this is called when page finishes loading.
I have loaded a web page in WebView via WebView.loadData().
Now I want to ad custom action to a button of that webpage from the WebView (Like, when I click the button, it will behave differently from the original webpage).
Is it possible from WebView? If possible, how can I do that?
Yes, it is possible. You can try something like this
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equalsIgnoreCase("your url"))
{
// your code
return true;
}
return false;
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
}
}
Don't forget to add myWebClient to your webView
No, it's no possible. You can only webView.setWebViewClient(new CustomWebClient());
private class CustomWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url...) // your logic here
return true;
}