Reputation: 13
For example if I have 3 buttons and I want to use this buttons to redirect to different urls without use many webviews but only one webview.
button1 = http://example.com
button2 = http://stackoverflow.com
button3 = http://android.com
Upvotes: 1
Views: 180
Reputation: 109237
Use HashMap with Integer, String key values pair,
Like Map<Integer,String> webUrls = new HashMap<Integer,String>();
Now store button's id as key and url as Values.
webUrls.put(button1.getId(),"http://facebook.com");
And load like, in onClick()
of Button
String url = webUrls.get(view.getId());
webView.loadUrl(url);
Now, you don't have to write onClick for every Buttons. Just in onClick() you can get Url from HashMap.
Upvotes: 1
Reputation: 27748
On the click of each Button
:
Intent intent = new Intent(context, THE_ACTIVITY_THAT_HOLDS_WEBVIEW.class);
intent.setData(Uri.parse("http://www.facebook.com"));
startActivity(intent);
Change the URL that has to be passed to the WebView
depending on the Button
clicked.
In the Activity
that holds the WebView
:
WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(this.getIntent().getDataString());
Adapt the code if you are doing things a little differently.
Upvotes: 2