Closing HTML viewer from my app - android

I have a html file which I am opening by launching the HTML viewer. Is there a way to tell HTML viewer from my app to close the page?
Here is the code I am using to launch the html file
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File file = new File("/data/data/Sample.html");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/html");
startActivity(intent);

Yes i am sure you want to view this HTML file inside your application instead of launching native browser. If this is the case then create WebViewClient and set it inside the WebView.
For example:
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
and set it inside the webview as:
webview.setWebViewClient(new myWebViewClient ());
and then load that URL into the WebView actually. Here is the detailed example.

Do you need to close the web page or transfer the control to other activity..If you go by the second option I would recommend you to use finish(); method at the close method where you use because it takes to the previous activity or you can choose your own destined activity.

Related

Android webview open external links in own application

When external links are clicked on my website, I want them to open in the application it is connected to. But I am getting page not found error. For example, when instagram.com is clicked, it will open on Instagram.
You need to define a webView CLient like this:
private class MyWebViewClient extends WebViewClient {
//link opener
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ("yourHost.com".equals(Uri.parse(url).getHost()))
{
// This is my website, so do not override; let my WebView load the page
//loading.show();
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;
}
}
on set this webViewClient in your webView like: mywebView.setWebViewClient(new MyWebViewClient())

How to avoid webview links opening in the same webview

When a user clicks on a link in the webview. I want to show a dialog asking whether the user wants to view it in default browser, if he opts YES then he should be taken to default browser otherwise it shouldn't load the link at all.
But, the issue I am facing is, I could able to show the dialog inside run() of WebViewClient's overridden method shouldOverrideUrlLoading(). When the user opts YES I am doing startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url))); to show it in default browser. But, irrespective of the user selects for YES/NO it is showing the link in webview which I want to avoid. Any suggestions appreciated...TIA
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
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;
}
}
http://developer.android.com/guide/webapps/webview.html
I resolved the issue, by just reloading the same URL inside shouldOverrideUrlLoading() method. Thanks for your suggestions

Force Android WebView to open in new browser

Android has changed its default method for opening clicks, it now opens them in the webview instead of a new browser. This has already been asked here but every thing I have tried opens the links in the WebView. Can someone give me details on capturing the clicks so I con force the link to open in the default browser.
use this in your button onclick:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
For it to parse it needs a http:// at the start.
Finally working don't know if it is the best way but it works. I placed the following code inside the onCreate. The string strSiteUrl set to the page I want the WebView to show.
/* Load WebView in memory */
WebView webv = (WebView) findViewById(R.id.webv);
webv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent browserIntent = new Intent();
browserIntent.setAction(Intent.ACTION_VIEW);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
browserIntent.setData(Uri.parse(url));
startActivity(browserIntent);
return false;
}
}); //End webv.setVewView
/* Configure WebView */
WebSettings webSettings = webv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
webv.loadUrl(strSiteUrl);
When a user clicked on a link in the WebView page it would open the default browser and show the linked page. However, after clicking the back button depending on which link was clicked the WebView would return to the original page or show the linked page. This is not what I wanted, I only wanted the WebView to show the original page. I don't know why some links didn't return correctly, maybe those links were redirects? So to get around this problem I used the onStart call. I made the view webv global by placing
WebView webv;
in my global declarations. Changed the webv assignment to
webv = (WebView) findViewById(R.id.webv);
Then created the following onStart
#Override
public void onStart() {
super.onStart();
String strReturnUrl = String.valueOf(webv.getUrl());
Log.i("URL!", strReturnUrl);
if (!strReturnUrl.contentEquals(strSiteUrl)) {
webv.loadUrl(strSiteUrl);
}
}
Writing to the log the returned url proved that when the back button was pressed it returned with different urls depending on which link was clicked. I used the if statement to prevent unnecessary reloading of the original url.

Open webview in separate browser on each click?

I am preparing sample app based on Web View. In my Splash screen have to load on url,if i click on splash screen it will open another site.It is working fine.But when i click on splash screen i want open separate browser.For that i have used following code,
this.webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
But it is opening same browser.Please Guide me.
view.loadUrl(url) will open the url in the same WebView. You have to define rule when to load in the same WebView or another seperate WebView or Browser. To load in seperate webview call anotherWebView.loadUrl(url). Or browser call with Intent.ACTION_VIEW.
The workaround could be like this-
if(need to load same webView)
{
view.loadUrl(url);
}
else if(need to load same webView)
{
anotherWebView.loadUrl(url);
}
else
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}

How to open WebView link to new activity?

How do I open link in webpage to new activity (what contains WebView aswell)?
I have webpage where is list and every list item contain different link. So I want that when user press first item it opens second activity and load that link to second activity's WebView. Hope you understand what I try to ask :)
Is that possible?
You can override the URL link clicks and open an activity on each click:
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Intent intent = new Intent(...);
startActivity(intent);
return true;
}
});

Categories

Resources