Currently the action mode toolbar shows above the toolbar, moving the whole layout down, and I want it to show on top of my current toolbar. I tried all solutions in this post and this one:
windowActionModeOverlay
with and without android:
prefix, no successview.ActionMode
and support.v7.view.ActionMode
, no successsetContentView
is called before super.onCreate
, this is not the problemRight now I just set toolbar visibility manually, which looks awful. Also I set the activity theme before super.onCreate
, could it be the problem? If not what is it? How can I make the attribute work?
My activity:
import android.support.v7.view.ActionMode;
import android.support.v7.widget.Toolbar;
// import ...
public class FileExplorerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_NoActionBar);
super.onCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// ...
}
private void setActionMode(boolean enabled) {
if (enabled) {
actionMode = startSupportActionMode(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.cab, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// ... change icons color for theme
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// ... handle item clicks
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {}
});
} else {
actionMode.finish();
}
}
}
My theme:
<style name="Theme.NoActionBar" parent="Theme.AppCompat.NoActionBar">
<item name="android:actionModeBackground">?attr/colorPrimary</item>
<item name="android:actionModeSplitBackground">@null</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="windowActionModeOverlay">true</item>
</style>
I have tried all the combination and found that you are using values-v21 folder for defining styles for lollipop and above devices and you have forgot to add <item name="windowActionModeOverlay">true</item>
in styles.xml in values-v21 folder.
Just to make sure you are following the right path I have just posted a sample project on Github showing how to show ActionBar on the top of Custom toolbar.
You can also download the sample APK and try it.
Answering all the question you have posted in the question
I tried using windowActionModeOverlay with and without android: prefix, no success
You don't have to use android prefix. The windowActionModeOverlay is valid.
<item name="windowActionModeOverlay">true</item>
I tried both view.ActionMode and support.v7.view.ActionMode, no success
This is required as you are trying to use support lib which has a separate Action Bar namespace import android.support.v7.view.ActionMode;
I tried using startActionMode both on my toolbar and my activity, neither worked.
It works in both ways. If you want to look at example you can see it in the github repo.
setContentView is called before super.onCreate, this is not the problem
This isn't a problem. It will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With