I am trying to use the android.view.ActionMode
with the new android.support.v7.widget.Toolbar
, in addition to the traditional android.app.Act
So, after days going thru this thread, I finally got it working. I'd like to summarize what I did.
Note: This is the solution using a
Toolbar
to replace the defaultActionBar
inAppCompatActivity
.
AppTheme
: It tells android that you want your action mode to overlay the toolbar<item name="windowActionModeOverlay">true</item>
You have to use these imports in order to work with AppCompatActivity
:
import androidx.appcompat.view.ActionMode;
// or
import android.support.v7.view.ActionMode;
actionMode = startSupportActionMode(callback);
And not like so:
actionMode = startActionMode(callback);
You Activity creates the ActionMode on the toolbar automatically, because it's set as the supportActionToolbar. The style handles the dsiplaying as overlay.
Thanks to @Kuffs and @Lefty.
If you see the view tree,You will can write below code:
ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
traverseView(decorView);
/**
* traverse find actionmodebar
* @param root view
*/
public void traverseView(View root) {
if (root==null){
return;
}
if (root instanceof ActionBarContextView){
root.setVisibility(View.GONE);
return;
}
if ((root instanceof ViewGroup)) { // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
traverseView(viewGroup.getChildAt(i));
}
}
}
Do not start it on your activity, but on your toolbar. In you activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.startActionMode(mActionModeCallback)
and you have to use
<item name="windowActionModeOverlay">true</item>
in your theme as stated by Andre.