How to add action bar options menu in Android Fragments

I am trying to have an options menu in Android Snippets . ActionBar options menu does not appear in my snippets.

Here is my code, and I have the onCreateOptionsMenu() and onOptionSelected() functions. My code does not show any errors. But the options menu is not displayed.

 package org.reachout; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import org.general.R; public class ViewMessageFragment extends Fragment { /* (non-Javadoc) * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { // We have different layouts, and in one of them this // fragment containing frame doesn't exist. The fragment // may still be created from its saved state, but there is // no reason to try to create its view hierarchy because it // won't be displayed. Note this is not needed -- we could // just run the code below, where we would create and return // the view hierarchy; it would just never be used. return null; } return (LinearLayout)inflater.inflate(R.layout.viewmessages_tab_fragment_layout, container, false); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.askexperts_menu, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // handle item selection switch (item.getItemId()) { case R.id.action_settings: // do s.th. return true; default: return super.onOptionsItemSelected(item); } } } 
+58
android android-actionbar android-fragments android-optionsmenu
Sep 10 '13 at 8:44
source share
3 answers

You need to call setHasOptionsMenu(true) in onCreate() .

For backward compatibility, it's best to place this call as close as possible at the end of onCreate() or even later in onActivityCreated() or something like that.

See: https://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)

+81
Sep 10 '13 at 8:58
source share

I was late for an answer, but I think this is another solution that is not mentioned here, so I submit.

Step 1: Make the xml menu you want to add, as if I should add a filter action to my action bar to create an xml filter.xml . The main line to be noted is android: orderInCategory , this will show the action icon at the beginning or at the end, wherever you want to show. Another thing to note is the value, if the value is less than it will be displayed at the beginning, and if the value is larger, then it will be displayed last.

filter.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" > <item android:id="@+id/action_filter" android:title="@string/filter" android:orderInCategory="10" android:icon="@drawable/filter" app:showAsAction="ifRoom" /> </menu> 

Step 2: In onCreate (), the fragment method just placed the line below, as indicated, which is responsible for calling onCreateOptionsMenu (menu menu, MenuInflater blower) , as in Activity .

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

Step 3: Now add the onCreateOptionsMenu method, which will be redefined as:

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.filter, menu); // Use filter.xml from step 1 } 

Step 4: Now add the onOptionsItemSelected method, with which you can implement the logic, no matter what you do when you select the added action icon from the actionBar :

 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if(id == R.id.action_filter){ //Do whatever you want to do return true; } return super.onOptionsItemSelected(item); } 
+68
Aug 11 '15 at 7:38
source share

in AndroidManifest.xml set the topic naked like this:

 <activity android:name="your Fragment or activity" android:label="@string/xxxxxx" android:theme="@android:style/Theme.Holo" > 

-17
May 10 '15 at 18:54
source share



All Articles