8

My app uses the old-style options menu. Currently we target API 9. On the Galaxy S4 I notice the following behavior:

  1. User taps on the menu button. Menu is displayed as expected.
  2. User taps on "more" to access the overflow section. Overflow section is displayed as expected.
  3. User taps the back button to return to the original options menu. Original menu displayed as expected.

At this point, tapping on any element in the options menu (including the "More") option seems to have no effect. However the system is actually caching those taps. If the user taps the a menu five times (with no visible effect) then taps the back button to hide the menu, then taps the menu button to display the menu once again, all the cached taps take place right away. onOptionsMenuItemSelected() is called once for every time the user tapped that menu item.

This only happens on the S4 (and possibly other Samsung devices). Notably, I don't see this behavior using a Galaxy Nexus running stock 4.2.2.

Is this a bug with Samsung's customization of Android, or is it more likely I'm doing something wrong in my app and the stock Android code is just more forgiving?

jph
  • 2,181
  • 3
  • 30
  • 55
  • It should be noted I'm creating the menu the most simple way I can imagine: In onCreateOptionsMenu() I just call Menu.add() eight times with the titles "1" through "8". It doesn't seem to matter whether I call through to the super method; the behavior is the same either way. – jph Jun 22 '13 at 12:50
  • im using old-style options menu in my app and tested on galaxy S3 runing 4.2.1 and every thing perfect , unfortunately i don't have S4 , if that will be useful i will post my code – Android Stack Jun 23 '13 at 15:43
  • I started a thread on the Android Developers group related to this issue: https://groups.google.com/d/msg/android-developers/OEhu886jLho/N5bYqFXDPKkJ One commenter ran the sample code I provided and said it worked correctly on a "GT-I9505" model S4, whereas the device where I noticed the buggy behavior was a "SGH-I337" model. – jph Aug 08 '13 at 14:38

1 Answers1

3

The below code for customized menu and tested with galaxy S3 running jelly bean 4.2.1 and work perfectly ,

unfortunately i don't have S4 , but you can check it also this is without more button:

 public class CustomMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public boolean onCreateOptionsMenu(android.view.Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.cool_menu, menu);
    getLayoutInflater().setFactory(new Factory() {
        public View onCreateView(String name, Context context,
                AttributeSet attrs) {

            if (name.equalsIgnoreCase(
                    "com.android.internal.view.menu.IconMenuItemView")) {
                try {
                    LayoutInflater li = LayoutInflater.from(context);
                    final View view = li.createView(name, null, attrs);
                    new Handler().post(new Runnable() {
                        public void run() {
                            // set the background drawable if you want that
                            //or keep it default -- either an image, border
                            //gradient, drawable, etc.
                            view.setBackgroundResource(R.drawable.myimage);
                            ((TextView) view).setTextSize(20); 

                            // set the text color
                            Typeface face = Typeface.createFromAsset(
                                    getAssets(),"OldeEnglish.ttf");     
                            ((TextView) view).setTypeface(face);
                            ((TextView) view).setTextColor(Color.RED);
                        }
                    });
                    return view;
                } catch (InflateException e) {
                    //Handle any inflation exception here
                } catch (ClassNotFoundException e) {
                    //Handle any ClassNotFoundException here
                }
            }
            return null;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.AboutUs:
        Intent i = new Intent("com.test.demo.ABOUT");
        startActivity(i);
        break;
    case R.id.preferences:
        Intent p = new Intent("com.test.demo.PREFS");
        startActivity(p);
        break;
    case R.id.exit:
        finish();
        break;
    }
    return false;
  }
 }

Dont forget to create folder called menu in res folder, and inside the menu folder create an XML for your menu (e.g. cool_menu.xml) such as this:

  <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item  android:title="about"android:id="@+id/AboutUs" /> 
   <item android:title="Prefs" android:id="@+id/preferences" /> 
   <item android:title="Exit" android:id="@+id/exit" /> 
 </menu>

Hope that will help.

Android Stack
  • 4,314
  • 6
  • 31
  • 49
  • I'll try it out on Monday. Out of curiosity, do you set a custom factory on the layout inflater just so you can change the default look and feel of the menu? That's not something I do currently. I wonder if your doing so ends up working around the problematic Samsung code. – jph Jun 23 '13 at 18:27
  • @user190758 yes i do and result is nice – Android Stack Jun 23 '13 at 18:33
  • For fun, could you add some additional menu items so that Android creates the "overflow" menu and see if you see the same bad behavior I do on the S4? With more than six menu items it should create the overflow menu. – jph Jun 23 '13 at 18:58
  • No love on the S4. I tested your code on the S4 and it exhibited the same buggy behavior. I then tested it on an S3 and it worked just fine; no buggy behavior. So it looks like this is an issue specific to the S4 and not "all Samsung devices". The S3 is running 4.1.1. The S4 is running 4.2.2. The Galaxy Nexus (which doesn't exhibit the buggy behavior) is running 4.2.2. So it's not an issue with 4.2.2. per se, but with the S4 specifically. – jph Jun 24 '13 at 15:14