JR Galia
JR Galia

Reputation: 17269

Displaying Android Action Bar

I'm new to android programming and I found out that menu are not available for android 3.0 and up. And suggestions is to use Action Bar. So I have the following in my AndroidManifest.xml

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="Main"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden|screenSize"> 

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

In my main.xml for menu/action bar:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/homeMenu"
        android:orderInCategory="100"
        android:title="@string/home" android:showAsAction="ifRoom"/>

    <item
        android:id="@+id/searchMenu"
        android:orderInCategory="200"
        android:title="@string/search" android:showAsAction="ifRoom"/>

    <item
        android:id="@+id/exitMenu"
        android:orderInCategory="300"
        android:title="@string/exit" android:showAsAction="ifRoom"/>

</menu>

But it only display activity title in action bar for Android ICS. What I want if possible is to have the menu for Android < 3.0 and Action Bar for Android 3.0 and up. Or if there are better approach for this.

I have onCreateOptionsMenu in my main activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

The menu works perfectly for Android 2.3.6 but Action Bar didn't display as I expected for Android ICS.

Thanks

Upvotes: 0

Views: 365

Answers (2)

user2260168
user2260168

Reputation: 126

try this

android:showAsAction="always"

instead of

android:showAsAction="ifRoom"

Upvotes: 1

britzl
britzl

Reputation: 10242

If you want a consistent look across Android versions you really should look into ActionBarSherlock

"The library will automatically use the native action bar when appropriate or will automatically wrap a custom implementation around your layouts. This allows you to easily develop an application with an action bar for every version of Android from 2.x and up."

Upvotes: 2

Related Questions