This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
android webview click opens default browser
I have an application that opens a website in webview on start, however I can't seem to figure out how to keep any URL's that a user clicked on from opening in the browser.
Is there anyway to load the clicked URL inside the webview app?
EDIT - Using the below solution I still get an error on the private class "Illegal modifier for the local class MainScreenActivity; only abstract or final is permitted"... even if I try to rename this I get the same error
public class MainScreenActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.talk-of-the-tyne.co.uk");
setContentView(webview);
private class MainScreenActivity extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
}
Apologies if I am being thick about this, I just can't seem to get my head around this one
try this code:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
Related
I am trying to get current html page path from "asset" folder in cordova android app, when we changing the pages inside the cordova webview.
I am using only MainActivity
public class MainActivity extends CordovaActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadUrl(launchUrl);
}}
I searched and tried my level best to find the solution. But still now I cannot get the right solution. I am totally confused, Please suggest any solution for the followings,
Any function available to detect cordova webview URL when page changed
like public boolean shouldOverrideUrlLoading(WebView view, String url) {
} method?
Can we create WebViewClient for Cordova Webview?
You can get the current url with
appView.getUrl();
To get url changes you can use this:
SystemWebView wv = (SystemWebView)appView.getView();
wv.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)appView.getEngine()){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// you can get the current url with view.getUrl()
// or the url that it's trying to load next with url
return false;
}
});
I have a problem. I created some app, which should open my website, but when I run the app on my phone, its ask me in which browser I want to open it(Google Chrome...). But I want that website is open in app, not in browser... I'm working with Android Studio 1.1.0... Here is my code in MainActivity.java:
public class MainActivity extends ActionBarActivity {
private WebView MyWeb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyWeb = (WebView) findViewById(R.id.WebView);
MyWeb.getSettings().setJavaScriptEnabled(true);
MyWeb.getSettings().setSaveFormData(true);
MyWeb.loadUrl("http://www.mywebsite.com");
} }
P.S. I changed the url to the fake one...
Add this line of code before your MyWeb.loadUrl
MyWeb.setWebViewClient(new WebViewClient());
This worked for me.!
I have a native android app in which there are two activities, 1st activity contains a button and on action of button I want to start the 2nd activity. Content of seconds activity will be from a website. Is it possible to make the 2nd activity behave like webapp ?
If I use WebView in this case, it is displaying the website inside the activity but when I try to interact with the website, it ask to open it in the browser which I don't want. I want to interact with the website within the activity.
Any help will be highly appreciated.
Thanks a ton.
Use below code... for open link in WebView itself instead of browser...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
WebView engine = (WebView) findViewById(R.id.web_view);
engine.loadUrl("https://play.google.com/store?hl=en");
engine.setWebViewClient( new HelloWebViewClient() );
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
return false;
}
}
see this link for more details... WebView should not open links in browser
I think if you set the baseURL property when creating the Webview, you'll get what you want.
Create you're own Webview where you can override the default behaviour:
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In my class my webview is extending from WebViewClient
the code snippet is given :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
view = (WebView)findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("http://192.168.1.102:8086/QOSKO/");
view.setWebViewClient(new HelloWebChrome());
}
private class HelloWebChrome extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
In this i am trying to open website into my application with the help of WebViewClient class .
this code is running on android api level 8 but the same code is not running on android api level 15.
my ques is how to run WebViewClient class on android api level 15.
I think ur problem is this :
view.loadUrl(url);
bcz u have load url two times first is view.loadUrl("http://192.168.1.102:8086/QOSKO/"); and other is view.loadUrl(url); in webviewchromeclient that'y exception occurs try to remove one of line from this both one by one
and check
This is an open issues
sol1: you need to add the webview to the view dinamically.
currently you are using view = (WebView)findViewById(R.id.webView1);
WebView view=new WebView(this);
sol2: Add android:hardwareAccelerated="true" to the actual Activity that the Webview was contained in the Manifest. Hopefully that works for you I think.
How can we get info from a webpage(link on it that downloads an excel file when you click on it)? What subjects should be searched in order to do that on Android? Thanks.
AFAIK, you will not able to know what are the link is for. There is a way to know about the link of the webview on which user clicks and it is WebViewClient.
Here is a code using which you can know that the clicked link has XLS file or pdf file or others. You can also block the link using that code
public class TestingActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView view = new WebView(getApplicationContext());
view.setWebViewClient(new MwebViewClient());
}
class MwebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".xls")) {
Log.d("Blah", "This link contains XLS file");
}
return super.shouldOverrideUrlLoading(view, url);
}
}
}