1

I am creating a very simple action bar. I have just added 2 items in the menu but still it does not work for Android 4.1. It does work on nexus 5 emulator though.

menu_main.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/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_exit"
        android:title="Exit"
        app:showAsAction="never"/>

</menu>

MainActivity.java

package com.mycompany.actionbaractivitypractise;


import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 } 


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

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
 }
}

Any comments or suggestions will be helpful. Thank you

Maveňツ
  • 1
  • 12
  • 50
  • 89
  • Newer phones, with no hardware menu button automatically add an Overflow menu icon to the Action Bar. but in your case The phones which have menu hardware button show extra menu items on click of the hardware button. – Maveňツ Mar 04 '15 at 07:17
  • Can Try http://stackoverflow.com/a/15493649/1761003 – Maveňツ Mar 04 '15 at 07:17
  • You should try adding `android:orderInCategory` attribute in menu items – Apurva Mar 04 '15 at 07:18

1 Answers1

-2

Your java code is correct but in menu_menu.xml instead of app: use android it will work :
Example:

<item
    android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:showAsAction="never"/>
<item
    android:id="@+id/action_exit"
    android:title="Exit"
    android:showAsAction="never"/>

KulArtist
  • 965
  • 8
  • 20
  • `Should use app:showAsAction with the appcompat library with xmlns:app="http://schemas.android.com/apk/res-auto` error message by Android Studio – Mr.Chowdary Jun 27 '15 at 17:25