I'm attempting to put a menu into a fragment in my app. However, the menu isn't appearing when I run it. My understanding of the steps involved in making a menu display in a fragment (and please correct me if I'm wrong or missing something) is that you do the following:
onCreateOptionsMenu(Menu, MenuInflater)
and within said method, inflate the layout defined by the menu resource ID.onCreateOptionsMenu
by calling setHasOptionsMenu(true)
in the fragment's onCreate
method.I've written a reduced version of my code to only include the bare minimum of what (I believe) should show a menu. Can anyone tell me what's missing from this code?
Here is my menu resource xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_1"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/menu_item_text"
android:showAsAction="ifRoom|withText"/>
</menu>
My fragment code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, parent, false);
return v;
}
}
And my activity code:
package com.bignerdranch.android.fragmentmenuexample;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = new MainFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
And my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.fragmentmenuexample" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
I'm running this on an AVD Galaxy Nexus running IceCreamSandwich.
Problem solved. My styles.xml file had the following:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I added a new resource directory values-v14 and added the following style to it:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With