Jose Ortiz
Jose Ortiz

Reputation: 311

How to display menu options in android?

I am having trouble displaying my menu options on my phone. I got all the code right I think but it is not displaying. Any help would be greatly appreciated.

Here is how my manifest looks like:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.drumloopsequencer"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:screenOrientation="landscape"
        android:name=".Main"
        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>

Here is the onCreateOptionsMenu function:

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

and here is the menu xml:

  <?xml version="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android" >

  <item 
    android:id="@+id/file"
    android:title="@string/fileTab"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    />

  <item 
    android:id="@+id/packs"
    android:title="@string/packsTab"
    android:orderInCategory="2"
    android:showAsAction="ifRoom"
     /> 

  <item 
    android:id="@+id/drumRack"
    android:title="@string/drumRackTab"
    android:orderInCategory="3"
    android:showAsAction="ifRoom"
    />

  </menu>

Upvotes: 0

Views: 1229

Answers (3)

Andrea Thacker
Andrea Thacker

Reputation: 3470

If you are using a fragment make sure that you have enabled the options menu with setHasOptionsMenu(true). A good place to call this might be from onCreate.

public class MyFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
}

or for an Activity

public class MyActivity extends Activity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mymenu, menu);
        return true;
    }

}

Upvotes: 1

Emmanuel Padrixe
Emmanuel Padrixe

Reputation: 58

Try this :

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow()
openOptionsMenu(); 
};

Upvotes: 0

Setu Kumar Basak
Setu Kumar Basak

Reputation: 12022

If you are using fragment then you have to do this:

//inside constructor
 public MyClassFrag(){
      setHasOptionsMenu(true);
}

or inside onCreate()

 @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

Upvotes: 0

Related Questions