9

I am trying to add actions item in action bar, but items always are adding into the overflow list even there is lot of room. Any suggestion to resolve this ??

enter image description here

This should be like this

enter image description here

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:showAsAction="ifRoom"
        android:title="@string/action_refresh" />

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="ifRoom"
        android:title="@string/action_search" />

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


</menu>

MainActivity.java

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.main, menu);
        return true;
    }

}

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.actionbardemo.app.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>

build.gradle

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
Braiam
  • 1
  • 11
  • 47
  • 78
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • if there is not enough space it appears in the overflow menu. its seems there is enough space but its not – Raghunandan Apr 03 '14 at 06:09
  • @Raghunandan that why I asked question here – N Sharma Apr 03 '14 at 06:11
  • Read this http://developer.android.com/guide/topics/ui/actionbar.html – Raghunandan Apr 03 '14 at 06:12
  • @Raghunandan I have read that, I could not trace my problem – N Sharma Apr 03 '14 at 06:13
  • there is no space hence it does not appear to you it looks like there is enough space but its not – Raghunandan Apr 03 '14 at 06:13
  • @Williams Check this link http://stackoverflow.com/questions/17914017/android-4-3-menu-item-showasaction-always-ignored – Zohra Khan Apr 03 '14 at 06:15
  • Are you sure that there is space for "Refresh" text ? Try with a single dummy letter. I think it is due to unavailability of Room for the text. – Aneez Apr 03 '14 at 06:15
  • 1
    @Williams the other thing is your min sdk is 8. so you need `xmlns:yourapp="http://schemas.android.com/apk/res-auto" ` and this `yourapp:showAsAction="ifRoom" ` – Raghunandan Apr 03 '14 at 06:17
  • I tried an example with the showAs=ifRoom and all the items gets displayed fine in the action bar. Not sure why it is causing an issue for you. The program is same as yours with the versions. – AndroGeek Apr 03 '14 at 06:17

7 Answers7

8

My app is using the Support Library for compatibility on versions as low as Android 2.2 so I have to define my own XML namespace and use that namespace as the attribute prefix

Solution was this, I was writing this android:showAsAction="ifRoom" but I should have app:showAsAction="ifRoom"

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        app:showAsAction="ifRoom"
        android:title="@string/action_refresh" />

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="ifRoom"
        android:title="@string/action_search" />

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


</menu>
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • that is bcoz your min sdk is 8 and you use AppCompat to support actionbar below api level 11. do mention the reason in your post – Raghunandan Apr 03 '14 at 06:19
5

in menu xml file you need to add yourapp:showAsAction="always" like below.

<item
    android:id="@+id/Option"
    android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    android:icon="@drawable/ic_action_settings"
    android:title="Option"
    yourapp:showAsAction="always"/>

<item
    android:id="@+id/Save"
    android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    android:icon="@drawable/ic_action_save"
    android:title="Save"
    yourapp:showAsAction="always"/>

PankajAndroid
  • 2,689
  • 3
  • 27
  • 41
4

As per the document

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
2

try this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_action_refresh"
    app:showAsAction="ifRoom"
    android:title="@string/action_refresh" />

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="ifRoom"
    android:title="@string/action_search" />

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

hosseinAmini
  • 2,184
  • 2
  • 20
  • 46
1

If you want to show all items always in actionbar then make your menu xml file like below:

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_action_refresh"
    app:showAsAction="always"
    android:title="@string/action_refresh" />

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="always"
    android:title="@string/action_search" />

<item
    android:id="@+id/action_settings"
    android:title="@string/action_settings"
    app:showAsAction="always" />

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

I have the similar issue before you should try this

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_refresh"
    app:showAsAction="never"
    android:title="@string/action_refresh" />

<item
    android:id="@+id/action_search"
    app:showAsAction="never"
    android:title="@string/action_search" />

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

Cyd
  • 1,245
  • 1
  • 14
  • 17
-4

Try with android:showAsAction="always" for the items in the menu.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
AndroGeek
  • 1,280
  • 3
  • 13
  • 25
  • 1
    @AndroGeek its a short answer that doesn't explain why it *should* work or what basis it has, its just a 'try' *and* apparently it didn't work. –  Apr 04 '14 at 14:18