-1

Hello there guys,

I'm new to Android, I tried to add a Menu to my Android application something like this : ActionBar

The problem is that i'm not getting the menu on the action bar, I'm getting it down without icon.

Here is my code :

  public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.layout.menu, menu);
    return super.onCreateOptionsMenu(menu);
}


@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
     switch (item.getItemId())
     {
     case R.id.chat:
        // Single menu item is selected do something
        // Ex: launching new activity/screen or show alert message
         Toast.makeText(MainActivity.this, "Chat is Selected", Toast.LENGTH_SHORT).show();
         return true;


     default:
         return super.onOptionsItemSelected(item);
     }

}

and here is the xml file :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/chat"
      android:icon="@drawable/ic_action_chat"
      android:title="action_search"
      android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->

Here is a screenshot of the application as you can see the menu is DOWN and without icon :

enter image description here

Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.menu"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
Nadador Base
  • 99
  • 10

2 Answers2

1

Change the android:showAsAction in your menu XML file from android:showAsAction="ifRoom" to android:showAsAction="always". That should work.

Update

Change inflater.inflate(R.layout.menu, menu); to inflater.inflate(R.menu.main, menu); and use the main.xml file in the menu folder instead of the menu.xml file in your layout folder.

Update 2

Since you mentioned in your comment that you are using the appcompat library, you should modify your main.xml file in your menu folder as follows:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:compat="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >
<!-- Search, should appear as action button -->

<item android:id="@+id/chat"
      android:icon="@drawable/ic_action_chat"
      android:title="action_search"
      compat:showAsAction="always" />
<!-- Settings, should always be in the overflow -->

</menu>
iRuth
  • 2,737
  • 3
  • 27
  • 32
0

Move your menu to the menu folder and do it like:

inflater.inflate(R.menu.menu, menu);
vinitius
  • 3,212
  • 2
  • 17
  • 22