action bar not showing options menu in jelly bean

1.9k Views Asked by At

Action bar does not show options menu in jelly bean,while it is shown in lollipop. here is my styles.xml`

 <resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#00796B</item>
    <item name="colorPrimaryDark">#004D40</item>
    <item name="colorAccent">#c51162</item>
   <item name="android:textColorPrimary">@color/abc_primary_text_disable_only_material_dark</item>


</style>


 </resources>` 

here is my app gradle

 compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.example.dell.syncytium"
    minSdkVersion 16
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
   }

My MainActivity extends AppCompatActivity. please help me.thanks in advance

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, logoutMenu, 0, "Logout");
    menu.add(0,Play,1,"Play Music");
    menu.add(0,Stop,2,"Stop Music");
    return result;
  }
2

There are 2 best solutions below

0
Iamat8 On BEST ANSWER

menu.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" 
 tools:context=".MainActivity">
<item
    android:id="@+id/connect_scan"
    android:icon="@drawable/menu"
    android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/discoverable"
    android:icon="@android:drawable/ic_menu_mylocation"
    android:showAsAction="ifRoom|withText"/>
</menu>

MainActivity.java

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.menu:
         break;
        case R.id.discoverable:
         break;
    }
    return false;
  }
}
4
Iamat8 On

You can make menu from your activity itself without menu.xml file but it will provide you single option on action bar.....

Try this..

 public class MainActivity extends Activity {


 @Override
 protected void onCreate(Bundle savedInstanceState) {
    // ---- //

 }
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, 1, "New");
        menu.add(0, 2, 2, "Create");
        menu.add(0, 3, 3, "Open");
        menu.add(0, 4, 4, "Delete");
        menu.add(0, 5, 5, "Exit");

        return true;
    }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case (1):
           //----//
            return true;
        case (2):
           //---//
            return true;
        case (3):
           //---//
            return true;
        case (4):
            //---//
            return true;
        case (5):
            finish();
            return true;

        }
        return false;
    }
 }

OutPut

enter image description here