0

I've got settings button at the right of my actionbar.

main.xml from rsc/menu:

  <item
    android:id="@+id/action_settings"
    android:icon="@drawable/settings"
    android:orderInCategory="1"     
    android:showAsAction="ifRoom"
    android:title="Settings" />
  <item
    android:id="@+id/action_help"
    android:orderInCategory="2"     
    android:showAsAction="withText"
    android:title="Help" />

I want menu with other items opens after pressing this button in actionbar. So I've added getActivity().openOptionsMenu() to my fragment in onOptionsItemSelected() function:

    @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            menu.clear();
            inflater.inflate(R.menu.main, menu);
            super.onCreateOptionsMenu(menu, inflater);
        }
        public boolean onOptionsItemSelected( MenuItem item) {

            switch (item.getItemId()) {

            case R.id.action_help:{
                //make help activity
            }
                return true;                       

            case R.id.action_settings:{

                getActivity().openOptionsMenu();
                }
                    return true;

            default:
                return super.onOptionsItemSelected(item);
            }

        }  

As I understood we can't use openOptionsMenu() in onCreateOptionsMenu() But how to solve this issue by another way?

tadvas
  • 131
  • 1
  • 13
  • I need to have settings button in titlebar. And after pressing this button settings menu shows. – tadvas Mar 28 '15 at 12:03
  • try `startActivity(new Intent(Settings.ACTION_SETTINGS));` – Xcihnegn Mar 28 '15 at 13:04
  • thnx for replying, but it opens device's settings, but I need app's one. In other situations `openOptionsMenu()` works good! I need the same solution – tadvas Mar 28 '15 at 17:48
  • I don't know if this is your problem but in the fragment menu you have to setHasOptionsMenu in the fragment onCreate to get to the fragment menu inflater. see:http://stackoverflow.com/questions/8308695/android-options-menu-in-fragment and http://developer.android.com/reference/android/app/Fragment.html. – steven smith Mar 28 '15 at 18:57
  • I've already get it. Menu works good when I press button on phone, but I want to do it programmatically. – tadvas Mar 28 '15 at 19:15

0 Answers0