4

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?

aiqency
  • 1,015
  • 1
  • 8
  • 22
  • Does your theme really have no parent? I always thought it would have to inherit from (in this case) Theme.AppCompat.NoActionBar – Bö macht Blau Jan 27 '16 at 13:09
  • Well I edit my question with full style.xml and the manifest activity declaration – aiqency Jan 27 '16 at 13:52
  • So it seems that your activity theme indeed has no parent. What happens if you add *parent="Theme.AppCompat.Light.NoActionBar"* ? – Bö macht Blau Jan 27 '16 at 14:00
  • Gradle Build complain "No resource identifier found for attribute 'parent'". Just in case I saw that my toolbar inherit from AppTheme.PopupOverlay – aiqency Jan 27 '16 at 14:09
  • Ok just saw the solution I edit my question. Thanks 0X0nosugar – aiqency Jan 27 '16 at 14:23

2 Answers2

3

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
aiqency
  • 1,015
  • 1
  • 8
  • 22
  • 1
    From documentation for [onCreateActionMode()](http://developer.android.com/reference/android/view/ActionMode.Callback.html#onCreateActionMode%28android.view.ActionMode,%20android.view.Menu%29) "Returns: true if the action mode should be created, false if entering this mode should be aborted." – Bö macht Blau Jan 27 '16 at 14:39
1

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
}
lincollincol
  • 762
  • 2
  • 12
  • 23