Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show option menu in android 4.2

I am trying to create menu option in my test application.

I am able to see the menu when I set the theme in manifest to just default (The menu shows up in the top). If I set the theme in manifest to NoTitleBar. I can't see the menu option?

I want to get the menu when I set theme "NoTitleBar" in manifest.

How to fix it?

Below are things that I have used in my test application:

with Manifest:

  <uses-sdk
      android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >
    <activity
        android:name="com.ssn.menuoptions.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>

And

Menu.xml

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/menu_preferences"
      android:title="Preferences" /> 
 </menu>

Java file:

@Override
public boolean onCreateOptionsMenu(Menu menu) 
    {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Is it possible to get something like this:

file:///C:/Users/Koushik/Downloads/SahayaPictures/fHEQ8.png

How do I make menu to show up down?

Thanks!

like image 496
TheDevMan Avatar asked Oct 21 '22 05:10

TheDevMan


1 Answers

Add following code below Activity.setContentView();

 setContentView(R.layout.activity_detail);
    // set option menu if has no hardware menu key
    boolean hasMenu = ViewConfiguration.get(this).hasPermanentMenuKey();
    if(!hasMenu){
        //getWindow().setFlags(0x08000000, 0x08000000);
        try {
            getWindow().addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
          }
          catch (NoSuchFieldException e) {
            // Ignore since this field won't exist in most versions of Android
          }
          catch (IllegalAccessException e) {
            Log.w("Optionmenus", "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);
          }
    }
like image 125
Jonny Chen Avatar answered Oct 24 '22 04:10

Jonny Chen