By clicking a button in my activity, I am opening a webview using a url. From that website going to payment gateway. After completing payment it will be automatically redirect to website which I had opened in a webview. Now How should I go back to app again.Anybody give me better idea and ways to do that
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("returnToHome"))
finish();
return false; // then it is not handled by default action
}
If there is any url change you can find it with this method. You want to decide in which url you want to go back. By checking the parameter of the url you can go back..Hope this helps you..
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 having trouble improving my webview. I want there to be a page once the app opens were the user can set the url that the webview is permanently going to. It sounds very simple but I'm having a hard time.
mb you can first show dialog, where user should enter url, and this url set to web view
or start another activity, when user enter url
Create UrlActivity.java, where in onCreate you will do following thing.
take URL to be shown as a user input in EditText.
while firing intent to next activity, set that URL in intent's putExtra.
get this extra string in next activity using
String url = getIntent.getStringExtra("key of your urlstring");
and load this url in your webView.
I am trying to wrap a web application say www.xyz.com within a webview using shouldOverrideUrlLoading. I have two activity one that launches the application, this checks all the activity running and determines whether to launch new instance or not and other activity which wraps my application www.xyz.com in the webview.
I'm facing two problems:
When I press the sleep button and then again press it to open the device, or when the application is idle and went to sleep and I press the button to open the device, the application restarts (the webview restarts).
Also when link in the webapplication which opens someother site say for example www.abc.com doesn't work fine, it works for the first time and opens the site in external browser as desired, however on further click tries to open the site within the webview which is not desired.
I have googled this and found suggestion like the code below:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("abc")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
However, in my case for the first time the site www.abc.com is opened in external browser but for the second time its open within the webview, i've debugged it in eclipse and found the url value comes the same in subsequent cliks .
1) For the WebView restart problem, you need to add the following lines of code to your activity that contains the WebView
android:launchMode="singleInstance"
android:alwaysRetainTaskState="true"
Also see tutorials on how to save state of WebView in Bundle.
2) For your external browser problem, try
url.equals("www.abc.com")
instead of
url.contains("abc")
Hope that solves your problem :)
Previously when using a webview in android, clicking any link would force the "proper" browser to open and then web browsing would continue in that instance.
However this seems to no longer be the case. The following code results in all links staying inside the webview, but I actually want the links to launch a new browser instance. Did this change in 2.3 ?
Note I am asking the opposite of what most people ask (they ask how to keep all links inside the webview, I want them to launch outside)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView view = new WebView(this);
setContentView(view);
view.loadUrl("http://news.bbc.co.uk");
}
}
EDIT: Clarification - the first URL will load in the WebView and all subsequent clicks will open in a new browser. This is simplification from what I really want, but good enough.
Bascially the problem is, previously clicking a link would open a new browser session. Most people don't want that (hence the questions on here about it) but I do. However for some reason now it seems to load in the webview all the time (2.3 perhaps?)
Let me understand. Do you want to launch the first webpage in the same webview, while the other hyperlinks to go to the default browser, or you want the default browser itself to open for the first link clicked?
If it's the first case, I don't exactly know, unless you know some way to gather the link URL from the webview.
For the second instance, i.e. launching the default browser for any URL click, just skip (remove) this line: view.setWebViewClient(new WebViewClient() { });, which will open all the links in the default browser, and not the webview itself.
Houps, wrong answer, I misunderstood your question.
Need to delete...
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.