Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't show actionMode on top of Toolbar in any way

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:

  • I tried using windowActionModeOverlay with and without android: prefix, no success
  • I tried both view.ActionMode and support.v7.view.ActionMode, no success
  • I tried using startActionMode both on my toolbar and my activity, neither worked.
  • setContentView is called before super.onCreate, this is not the problem

Right 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>
like image 433
Nicolas Avatar asked Jan 12 '17 00:01

Nicolas


Video Answer


1 Answers

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.

like image 92
Dharmendra Pratap Singh Avatar answered Oct 02 '22 23:10

Dharmendra Pratap Singh