Yann Huissoud
Yann Huissoud

Reputation: 1033

Contextual Action Mode AppCompatActivity not show

I have my activity extending AppCompatActivity, and I wish to set a contextual action bar on it. So here is my onCreate method

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MainActivity.this.startSupportActionMode(new ActionBarCallBack());

My ActionBarCallBack extend android.support.v7.view.ActionMode and I declared it like this

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
    return false;
}

The theme set on my manifest:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionModeOverlay">true</item>
</style>

MainActivity:

<activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main2"
        android:theme="@style/AppTheme.NoActionBar" >
</activity>

My onCreateActionMode trigger but the CAB never show.

Any ideas?

Upvotes: 4

Views: 1853

Answers (2)

lincollincol
lincollincol

Reputation: 862

You return false from implemented onCreateActionMode method in your ActionMode.Callback.

Try to change the return value to true

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate menu
    return true; //  <=====  MUST RETURN TRUE
}

Upvotes: 1

Yann Huissoud
Yann Huissoud

Reputation: 1033

So here was the error

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
    return true; // Now it works
}

Set the windowActionModeOverlay to my Theme.NoActionBar

<item name="windowActionModeOverlay">true</item>

Don't have to set the startActionMode() from my toolBar as I saw on over stackoverflow thread. On android.support.v7.view.ActionMode this line just work fine for me.

Main2Activity.this.startSupportActionMode(new ActionBarCallBack()); //android.support.v7.view.ActionMode

Upvotes: 3

Related Questions