Why is the Android context menu empty?

Viewed 118

I have the context menu layout:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
  <item
        android:id="@+id/share_btn"
        android:icon="@drawable/ic_delete_black_24dp"
        android:title=""
        app:showAsAction="always"/>
  <item
        android:id="@+id/edit_btn"
        android:icon="@drawable/ic_edit_black_24dp"
        android:title=""
        app:showAsAction="always"/>
  <item
        android:id="@+id/remove_btn"
        android:icon="@drawable/ic_delete_black_24dp"
        android:title=""
        app:showAsAction="always"/>
</menu>

And Fragment, where I am trying to to show the menu:

class OverviewFragment: Fragment() {

/* some code */

override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenu.ContextMenuInfo?) {
    super.onCreateContextMenu(menu, v, menuInfo)
    this.activity!!.menuInflater.inflate(R.menu.binge_card_menu, menu)
 }
/*some code */ }

But the layout didn't show:

enter image description here

But listeners worked, and if I make the menu programmatically everything shows correctly, so I don't know why.

3 Answers

It happens because you are using empty string as title:

 <item
    android:id="@+id/share_btn"
    android:title=""
    .../>

Correct way:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
  <item
        android:id="@+id/share_btn"
        android:icon="@drawable/ic_delete_black_24dp"
        android:title="Share"
        app:showAsAction="always"/>
  <item
        android:id="@+id/edit_btn"
        android:icon="@drawable/ic_edit_black_24dp"
        android:title="Edit"
        app:showAsAction="always"/>
  <item
        android:id="@+id/remove_btn"
        android:icon="@drawable/ic_delete_black_24dp"
        android:title="Remove"
        app:showAsAction="always"/>
</menu>

Don't use empty string.

<item
android:id="@+id/share_btn"
**android:title="Share"**
.../>
Related