Question

I am trying to add menu item to action bar. menu/main.xml:

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

    <item
        android:id="@+id/change_language"
        android:showAsAction="always"
        android:title="JP">
    </item>
    <item
        android:id="@+id/lists"
        android:icon="@android:drawable/ic_menu_agenda"
        android:showAsAction="always"
        android:title="Lists">
    </item>
    <item
        android:id="@+id/collections"
        android:icon="@android:drawable/ic_menu_star"
        android:showAsAction="ifRoom"
        android:title="Collections">
    </item>

</menu>

change_language, lists items are working perfectly. Menu item "collections" is not showing

MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.change_language).setTitle("JP");
    return true;
}

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

It is not giving any error. I have checked R.java. There is no reference to "collections". Intellicence also not giving hints while writing "R.id. " How to add menu item to action bar? Maybe this is because, I published app to Google Play. However, I have set Debuggable to 'true'.

Was it helpful?

Solution

R.java doesn't contain id.collections, it only contains main.xml, like this :

public static final class menu {
    public static final int main=0x7f0e0000;
}

Also, your app publishing and this issue are not related.

Where is your R.id.change_language? If you wanna create customized actionbar with icon and title, you have to modify your main.xml and add custom layout like below :

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

<item
    android:id="@+id/collection"
    android:actionLayout="@layout/your_custom_layout"
    android:showAsAction="ifRoom|withText"
    android:title="ActionBar"/>

</menu>

your_custom_layout.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/your_imageview"
    android:background="@android:drawable/ic_menu_star" />

<TextView 
    android:id="@+id/change_language"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

then, you can findViewById in onCreateOptionsMenu function.

Hope this can help!

OTHER TIPS

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.

So add this to your tag ' xmlns:yourapp="http://schemas.android.com/apk/res-auto" '

and set the showAsAction attribute like this: yourapp:showAsAction="always" in your items

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