Question

Im trying to create a menu item so when a user clicks on the menu button on their phone it displays this menu. My code is compiling and its displaying a menu but not the image or text assoiciated with the menu button.

I have the image in a folder res/drawable/inage1icon.png Any Idea what the issue is?

Below is the code

package com.webview;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.view.Menu;
import android.view.MenuInflater;


public class WebViewActivity extends Activity { 

WebView mWebView; 

public boolean onCreateoptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu)
    return true;
    }
}

mainmenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/buttoneone" 
      android:icon="@drawable/image1icon"
      android:title="@string/showimage1" />
</menu>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, WebViewActivity!</string>
<string name="app_name">WebView</string>
<string name="showimage1">IMAGE ONE</string>

<color name="background">#000000</color>

</resources>
Was it helpful?

Solution

Is this a copying and pasting error, or is this the way your code is within the app?

You've misspelled the method name. It should be:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu)
    return true;
}

whereas you have public boolean onCreateoptionsMenu(Menu menu). Also, it is a good idea to return the superclass's method; instead of saying return true, say return(super.onCreateOptionsMenu()).

EDIT: Also, if you are developing in Eclipse, you can ensure that spelling errors such as this won't occur if you use the shortcut Cmd+Opt+s and select Override/Implement Methods. In that menu, Eclipse will list all the methods in the class you have extended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top