Reputation: 16219
I have created android app using webview and added menu's into it.
In main.xml code
<item android:id="@+id/interview_tips" android:title="@string/interview_tips"></item>
<item android:id="@+id/interview_tips" android:title="@string/interview_tips"></item>
<item android:id="@+id/career_trend" android:title="@string/career_trend"></item>
<item android:id="@+id/video_hub" android:title="@string/video_hub"></item>
which is giving me menus like following image.
when I clicked on any menu I want to open a URL in same webview, How can I do it
I tried like this which is not working
//menu click event
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mWebView.loadUrl("http://www.google.com");
return true;
case R.id.career_trend:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mWebView.loadUrl("http://www.google.com");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'm bit new in android please give me any link , tutorial?
Upvotes: 0
Views: 438
Reputation: 15821
Use onOptionItemSelected if you use a Menu.
Also look a Menus guide and an Example : How to use menus.
And using a WebView required INTERNET permission, add below into AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
Also you can try enabling webview JS.
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
First of all change your code
//menu click event
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
mWebView.loadUrl("http://www.google.com");
case R.id.career_trend:
mWebView.loadUrl("http://www.google.com");
}
return true;
}
//menu click event
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
return true;
case R.id.career_trend:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
return true;
}
return true;
}
Secondary - make sure that your webview is present on screen. I mean is visible...
Third - check INTERNET PERMISSION in your manifest file.
and LAST! - make sure that your menu click listener is working. Just put Log into onOptionItemSelected
method and check that he printed.
Also look WEB VIEW EXAMPLE : http://www.mkyong.com/android/android-webview-example/
Upvotes: 1
Reputation: 15382
String[] mListOfUrls = { "http://google.com", "http://go.com", "http://gle.com" };
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Log.wtf("URL", mListOfUrls[pos]);
webView.loadUrl(mListOfUrls[pos]);
}
Refer : Spinner Tutorial
Refer : Spinner methods
http://www.mkyong.com/android/android-spinner-drop-down-list-example/
Upvotes: 0