my android app is simple client for web site. It opens site's url, using JavaScript and DOM enters username and password (predefined) and clicks "Log in" button. It uses WebView class.
Question: how during that process I could show some "ajax" loading image instead of showing webweiv on the same activity?
THank you!
You can set a WebViewClient to your webview and use the callbacks onPagestart and onPageFinished to show the progress.
Related
I want to create a native Android application based on my existing website.
Due to extreme time constraints and the fact that we don't have an API server set up, I think it'd be fastest to just embed the website as a WebView in my mobile app.
So, here's what I had in mind for the mobile app:
The website has a login page.
I want that login page to be the the first screen that comes up when you enter the mobile app.
Once you hit the "sign in" button on the WebView login page and the login credentials are valid, I want it to take you to a new activity (I have no clue if this is possible since you have to detect the button click on the webview and then determine whether the credentials are valid).
Apologies, in case it's not painfully obvious, I'm not really a mobile developer. How would you guys go about this?
Does that next activity still contains WebView inside and just want display different buttons through another activity?
If that is the case, you can use this code
webview.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://Url displayed after successfull log-in"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
return true;
}
return false;
}
});
But if the case is after you logged - in through web, user will be redirected to Activity and will display data from WebView, it is impossible considering that you have no API to communicate with your Web App.
I am using Twitter4j but I need to authenticate a user with oAuth. The user needs to get a pin and input it into the app. When I load a webview with the url and the user accept's to give the app privileges, they receive a pin. How would they close the webview in order to input the pin. I cant close the webview in the emulator. Below is the method in the Async class where the webview is shown after the url is retrieved.
protected void onPostExecute(String url) {
WebView webview = new WebView(MainActivity.this);
setContentView(webview);
webview.loadUrl(url);
}
You can't "close" a WebView. What you could do is hide it. Maybe this solution will shed some light - Hide WebView until JavaScript is done
Well, you could
instead use another dedicated activity with mainly a webview [potentially also with 'done' button]. You could start that activity from your onPostExecute with intent, and then you will go back when user is done with it, or you could start that webview activity for result
Another option is to replace your webview when you are done with another view, for example by calling setContentView with another layout. I never did this myself, and not sure it will work easily, but I found that some people recommend using ViewFlipper for similar effect.
Hope that helps.
You can call finish() from anywhere inside the Activity class
This works for me:
webview.destroy();
Add a close button and on its click set:
webview.setVisibility(View.INVISIBLE);
webview.loadUrl("");
You can then reduce the height width of the web view to minimum.
In a WebView, how do you access the JavaScriptInterface from multiple web
pages, and not just from the page that you load via mWebView.loadUrl("http://10.241.139.45:9081/amexco/login.html")
e.g.
In my project's assets folder, I have:
login.html
page1.html
page2.html
... and here is the relevant WebView code:
webView.addJavascriptInterface(new JavaScriptInterface(), "android");
webView.loadUrl("file:///android_asset/index.html");
So at different points in time, a user may look at index.html, page1.html, etc. From what I can tell, only the JavaScript in index.html has access to the JavaScriptInterface via "window.android.someMethodName()". When I try to make a call to "window.android.someMethodName()" from page1.html, it says that "window.android" is not defined.
so the user starts the webview from
login.html page and navigates through different pages..
my requirement is in the last page of my webpage i have a button saying return to android activity. so when user clicks on the last page of my navigation the user has to come back to the android activity.
window.android.someMethodName()" can be called in html page which works fine. but this method will work if i call this method only from the first page which i loaded ie from the login.html page only this method will work.. if i call this method from the last page of the webview naviagation this is not working.
Can any one help me out in this.
Get rid of the window.. If you are injecting a virtual global named android, you call methods on it as android.someMethodName().
Also, make sure that you are viewing your subsequent Web pages in the WebView. By default, links bring up the user's default Web browser, which will not have your injected virtual global.
Is there anyway I can open web when user clicks on TextView, in a new window without leaving the app, it's like in iPhone when you click on an URL it shows you a new browser window, but there is a BACK button you can click to go back to previous screen?
Thanks!
You can create your own Browser Activity with a WebView in the layout. Inherently it is not going to have any of the functionality of the actual stock browser app though, other than displaying the web page. You'll have to add in the forward / back buttons if you want them and anything else you need. If you don't need any of that stuff then you should be fine with just a plain WebView. When you press the back button on the device it should close that activity and take you back to the one you started in.
Edit:
To get callbacks from a WebView when the user clicks a link you can use the WebViewClient shouldOverrideUrlLoading() method, like this:
wv.setWebViewClient(new WebViewClient() {
/* On Android 1.1 shouldOverrideUrlLoading() will be called every time the user clicks a link,
* but on Android 1.5 it will be called for every page load, even if it was caused by calling loadUrl()! */
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.i(myTag, url);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) ;
startActivity(i);
return true;
}
});
For a TextView its a little bit more work, You'll have to make your own copy of the Linkyfy class and use a TransformFilter to make the links behave however you want them to. Check out this question for an example Android Linkify both web and #mentions all in the same TextView
Maybe a WebView is what you want.
I have a webview with a banner and when I click on the banner there should open a second webview that follows the link. How can I do that? I have created the first WebView and it shows my banner but when I click on it, it opens the link in the same WebView.
How can I catch any events in the WebView when I click on a link that it should do something (with that link)? Just like shouldStartLoadWithRequest in iPhone.
Thank you,
Wouter
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
shouldOverrideUrlLoading good method, but if you click on current link webview client not calling to shouldOverrideUrlLoading method.