Question

I am trying to implement an activity with an action bar with the v7 support library. But the action bar just does not show. The menu items are showing as a normal menu list when i touch the menu button. What am i doing wrong?

Manifest.xml :

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".ChatSession_ActionBarActivity"
        android:theme="@style/Theme.AppCompat.Light">
        <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

chatsession_menu.xml :

<?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/dummy1"
            android:title="dummy1 menu option"
            android:showAsAction="always"
            android:icon="@drawable/ic_action_search">

        </item>
    </menu>

ChatSession_ActionBarActivity :

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

public class ChatSession_ActionBarActivity extends ActionBarActivity{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actionbaractivity_chatsession);
        getSupportActionBar().show(); //this line is not required, but just giving it a try

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.chatsession_menu, menu);
        return true;
    }
}
Was it helpful?

Solution

You need to have

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/dummy1"
        android:title="dummy1 menu option"
        yourapp:showAsAction="always"
        android:icon="@drawable/ic_action_search">

    </item>
</menu>

From the docs

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top