Kazuko Ando Kazuko Ando - 8 days ago 4
Android Question

Android webview ui

I'm beating my head against the wall here. I'm simply creating a webview that also has the capability to go forward, refresh and back. I've been playing with the Android dev kit for two weeks now and feel I'm fairly far however I can't seem to solve this problem no matter what I do.

I simply want to press the menu button and get a "Back", "Refresh" and "Forward" option. Thus far I have managed to have the buttons appear in my webview however they perform no action, as in nothing happens. Any solution for the newbie [who has searched everywhere] would be great.

My code is included below:
XXXXX.java--------------------------



package com.AFMoB.XXXXX;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.Menu;
import android.view.MenuItem;

public class XXXXX extends Activity
{ //** Called when the activity is first created. */
public WebView webView; //DECLARE webview variable outside of onCreate function so we can access it in other functions (menu)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Part of the progress bar system for future use--> this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true); // Enables Java
webView.setWebViewClient(new WebViewClient()); // Opens web links clicked by user in the webview
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) { // Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.afdc.energy.gov/afdc/locator/m/stations/");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
menu.add(0, 1, 0, "Back");
menu.add(0, 2, 0, "Refresh");
menu.add(0, 3, 0, "Forward");
return true; // End of menu configuration
}
public boolean onCreateOptionsMenu (MenuItem item){ // Called when you tap a menu item
switch (item.getItemId()){
case 1: //If the ID equals 1 , go back
webView.canGoBack();
item.setIcon(R.drawable.back); // Occurs after tapping the back menu item
return true;
case 2 : //If the ID equals 2 , go refresh
webView.reload();
item.setIcon(R.drawable.refresh);
return true;
case 3: //If the ID equals 3 , go forward
webView.canGoForward();
item.setIcon(R.drawable.forward);
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history
webView.goBack();
return true;
} // If it wasn't the BACK key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}

**main.xml**---------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<WebView
android:id="@+id/webview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:focusableInTouchMode="true">
</WebView>
<ProgressBar
android:id="@+id/myProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="30"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

**manifest**------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AFMoB.XXXXX"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET">
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".XXXXX"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

sat sat
Answer

The place where you have commented that
"// Called when you tap a menu item"
You are calling onCreateOptionsMenu,
it should be

public boolean onOptionsItemSelected(MenuItem item) {
}