Reputation: 75
I am trying to create my first Android app and need some help.
Basically the app has a webview which loads a URL which works fine but I have a button under it that I want when pressed to open a different URL in the webview window.
I cannot figure out how to tell the button to do this.
This is my first app so apologise if the answer is very simple.
Many thanks in advance
Jay
Upvotes: 1
Views: 349
Reputation: 29121
Assuming the button is inside the webView...
create a new class which extends webviewclient and add this to your web view like this.. , it will load the remaining urls in the same web view.
mWebView.setWebViewClient(new HelloWebViewClient());
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
take a look at this tutorial too....
if it is outside webView.. then
Button webButton = (Button) findViewById(R.id.yourbuttonId);
webButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mWebView.loadUrl(urltoload);
}
});
mWebView is the web view in your xml.you need to get access to it. Put the above code in onCreate
of your activity after you do setContentView()
Upvotes: 1
Reputation: 8304
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl(yourUrl);
}
});
Upvotes: 0