Android wevbiew ui

Home - Android wevbiew ui

Chintia Widyasari asked 10 months ago

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:

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);
            }
        }
2 Answers
Girly Saputri Staff answered 10 months ago

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) {
}
Chintia Widyasari answered 10 months ago

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.

Your Answer