I an using the WebView class to display tutorial in my apk. I like it as I can manage and update tutorial without making any changes in apk. I keep all tutorial html files in the specific folder on our website.
My question is there any events that will allow me to catch start navigation (when a user clicked on a link). I want to see on what link a user click in my tutorial and as long as a user navigate within the tutorial folder on our website he supposed to be navigating in WebView of my apk. However if there any link which lead outside of the tutorial folder then navigation to this link should be blocked and navigation should be passed to a browser.
I am not sure whether I explained it clearly. I am quite new to Android development. Please see below an example of my code in C# for Windows phone, I need to have the same in Android apk
/// <summary>
/// navigation in the application's browser goes as long as it is in the "stockscreenerapp" folder
/// if user goes out of this folder ("stockscreenerapp" cannot be found in the URL string) then
/// external default browser is called for further navigation
/// </summary>
private async void webBrowser1_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) {
if (null != args.Uri) {
string url = args.Uri.ToString();
int i = url.IndexOf("stockscreenerapp"); /* check whether "stockscreenerapp" is in the URL address */
if (i == -1) { args.Cancel = true; await Windows.System.Launcher.LaunchUriAsync(args.Uri); /*if it is not we call external browser and pass URL to it*/}
else { imgLoading.Visibility = Visibility.Visible; /* show loading image while page is loading */ }
}
}
Try using a WebViewClient and listening for onPageStarted. That way you can get the url of the clicked link, check if it's outside of your tutorial, and tell the WebView to stopLoading if it is.
In code, it might look something like this (warning, untested):
WebView webView = new WebView(context);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted (WebView view, String url, Bitmap favicon) {
if(Tutorial.doesNotContain(url))
view.stopLoading();
view.goBack();
}
});
Related
I wish to show only the login-page element of a URL. My current approach which is not working:
web.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:document.getElementByClassName('login-page')");
}
});
web.loadUrl("https://www.reddit.com/reddits/login");
For mobile screens you should develop separate screens and embed it.
Another way is if your web page supports p/# tag (i.e) like
https://developer.android.com/guide/components/fundamentals.html#Components
https://www.reddit.com/reddits#login this should navigate the page directly to login form
Anyhow I prefer you should develop the page for mobile screens.
Your approach is close to working, but you should modify the existing page instead of trying to load a subset of the existing page as a new one.
The following hides elements with class SectionToRemove, you could instead hide all and only make visible the area you need:
#Override
public void onPageFinished(final WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:$('.SectionToRemove').hide();");
}
That being said, Reddit has extensive APIs that should be used if possible, this approach may breach Terms of Service.
I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.
I am working on an android project right now and have a question about how to do callbacks in different webviews. I used JSInterface for my project too. Here I have 2 webviews. One has an index page, anther is a overlay(still a html page though.) What I want to do is if any user clicks on some links on the overlay, it should fire a callback function which is written in the java file where the index page was connected to through JSInterface. It might sound confusing, but I have draw something to help make it clear!
Thanks!
You can use a custom URL scheme like myurl://function for your functionality links. Then write an event handler for the WebView's shouldOverrideUrlLoading event in which you decide how to process the URL: either instruct the webview to load it, or do some custom action.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("myurl://"))
{
// Parse further to extract function and do custom action
}
else
{
// Load the page via the webview
view.loadUrl(url);
}
return true;
}
I used startsWith to check the URL for this quick and dirty example, but you should consider using android.net.Uri.parse for parsing URLs.
This should allow you to call the Java function foo() without having to go through the first WebView.
If you want to go through the first webview, then you can call a function on the JSInterface like this (where webView1 is the first WebView retrieved through findViewById):
webView1.loadUrl("javascript:myjsinterface.myjsfunc();")
I have an application in which i have added a menu.
Clicking on this menu opens up a website.
There is a list of links(zip files) available on this website.
Clicking on a particular link should result in that zip file to be downloaded to the assets folder of my application.
I am able to load the website.
Code for this:
String url = "http://almondmendoza.com/android-applications/";
Intent k = new Intent(Intent.ACTION_VIEW);
k.setData(Uri.parse(url));
startActivity(k);
I am referring to the example given on this website
What i am curious to know is that whether it is possible to perform an action on click of a particular link available on website. If it is possible then how can i accomplish this task?
Use WebView to load webpage, you can recognize URL using following code
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
webView.loadUrl(url);
// Here the String url hold 'Clicked URL'
return false;
}
});
I am trying to launch a websearch using data input from a user. The data is input through TextEdit boxes. Upon submission of the data, i would like my program to: 1) search for a specific webpage based on the user input 2)Find specific elements at the webpage 3) Display the webpage.
Here is an example:
User Input (in a non browser/webview page)
1) Store Name: Macey's 2)Zip Code: 77471
In the background my program will:
1) Find the Macey's website
2) Find the store nearest zip code 77471
3) Load the Web page for the store nearest zip code 77471
Obviously there is a lot of error handeling, exceptions, ect that would go along with this. For the sake of making this example "easy" lets pretend that 1) A the Macey's main page exists 2)A sperate page for the 77471 store exists. 3)There is a link to the 77471 store on the Macey's main page.
I have the code for getting the user input variables and i know how to launch the webview. What i dont know how to do is to search for the Macy's home page, then find the link i am looking for on the homepage and navigate to it. Loading the webview is not the problem. Find the data is.
Below is my current code. Right now i am setup so that the user will navigate to the webpage they are looking for but i would rather handle the searching for them, if it is possible.
public void InitializeWebView(){
portal = (WebView)findViewById(R.id.web_Portal);
WebSettings Settings = portal.getSettings();
Settings.setSavePassword(false);
Settings.setSaveFormData(false);
Settings.setJavaScriptEnabled(true);
Settings.setSupportZoom(true);
Settings.supportZoom();
portal.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
public void searchAndShow(String Store, String zip){
portal.loadUrl("http://www.google.com");
}
You can get search result in JSON format from google using their API. Here is a nice example in JAVA. Just don't use key parameter until you do not have a vlid key.