[FIXED] Setting a persistant android option menu

Issue

I am looking for a way to have the options menu persist on screen as soon as the activity is opened without pressing the menu button of the simulator and it should be there until one of the options in the menu is pressed

Currently I have made an option menu like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon"
    android:icon="@drawable/icon" />
<item android:id="@+id/text"
    android:title="Text" />
<item android:id="@+id/icontext"
    android:title="Icon and text"
    android:icon="@drawable/icon" />
</menu>

And my code is:

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


@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) 
    {
        case R.id.icon:     
                            break;
        case R.id.text: 
                            break;
        case R.id.icontext: 
                            break;
      }
      return true;
  }

Please suggest changes in my code.

Solution

Activity‘s option menu can be programatically opened and closed with the openOptionsMenu and closeOptionsMenu respectively

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

and to close:

closeOptionsMenu();

for lvls that do not support onAttachedToWindow could be used delayed execution(Not the best way):

    new Handler().postDelayed(new Runnable() {
        public void run() {
            openOptionsMenu();
        }
    }, 1000);

Answered By – user849998

Answer Checked By – Robin (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published