1

Im trying to get a custom menu to show when the menu button is clicked on my phone. Its not showing at all.

I have a register icon caled register.png in this folder /res/drawable. I have my my_menu.xml in a folder called /res/menu. Did I lay out my folders wrong or is there something wrong in my code below.

I renamed menu.xml to my_menu.xml I changed my code and now im getting these errors:

[2012-04-07 07:50:43 - HelloWebView] W/ResourceType( 1560): Bad XML block: no root element node found [2012-04-07 07:50:43 - HelloWebView] C:\Users\josh\workspace\HelloWebView\res\menu\my_menu.xml:4: error: No resource identifier found for attribute 'showAsAction' in package 'android'

my_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:id="@+id/register"          
        android:icon="@drawable/register"          
        android:title="@string/register"          
        android:showAsAction="ifRoom"/>    

</menu>

Mainapp

public class HelloWebViewActivity extends Activity {
    WebView mWebView;

    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);    
        mWebView = (WebView) findViewById(R.id.webview);    
        mWebView.getSettings().setJavaScriptEnabled(true);    
        mWebView.loadUrl("http://www.Google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());
    }

    private class HelloWebViewClient extends WebViewClient {   
        @Override    
        public boolean shouldOverrideUrlLoading(WebView view, String url) {        
            view.loadUrl(url);        
            return true;    
        }}

    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {    
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {        
            mWebView.goBack();       
            return true;    
        }    
        return super.onKeyDown(keyCode, event);}

    @Override
    public boolean onCreateOptionsMenu(Menu my_menu) {    
    MenuInflater inflater = getMenuInflater();    
    inflater.inflate(R.menu.menu, my_menu);    

    return true;
    }
}
CsharpBeginner
  • 1,753
  • 8
  • 38
  • 68

3 Answers3

4
inflater.inflate(R.menu.my_menu, my_menu); 

That solved the code alone with removing
android:showAsAction="ifRoom"

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
CsharpBeginner
  • 1,753
  • 8
  • 38
  • 68
2

I had a similar problem. I did not get any error, just the menu button didn't show up. I fixed the problem in Manifest.xml file by changing android:theme="@style/AppBaseTheme" (or any other theme compatible with minSDK) . Because I was messing with style.xml file and created my own. This caused the problem. May help.

Yanna
  • 21
  • 1
  • I had the same issue, except that I had this property defined not only for the , but for each . – Markon Apr 09 '16 at 09:11
1

Every thing look clean just clean and build your app. And also if these does not solve your problem once unistall the app and reinstall it.

also if your xml name is Menu.xml make it menu.xml... that is case sensitive

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • It is lower case. I do notice that this line of code may have an issue. inflater.inflate(R.menu.menu, menu); the second menu is in blue. Does my menu need to be named mymenu or something> – CsharpBeginner Apr 07 '12 at 11:58