CsharpBeginner
CsharpBeginner

Reputation: 1773

Android Menu not showing up

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;
    }
}

Upvotes: 1

Views: 12283

Answers (3)

CsharpBeginner
CsharpBeginner

Reputation: 1773

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

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

Upvotes: 4

Yanna
Yanna

Reputation: 21

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.

Upvotes: 2

Shankar Agarwal
Shankar Agarwal

Reputation: 34775

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

Upvotes: 1

Related Questions