Toolbar won't disappear in action mode
I am trying to implement the feature that, when I am long clicking a list item, the action mode shall start and it shall be possible to delete one or more items.
I am starting in the MainActivity DocumentsActivity a search, which starts a Fragment DocumentsFragment with the ListView and their items. The ListAdapter is initialized and set via method call setListAdapter(this.documentsAdapter) in the onCreate in the Fragment. I set various listeners on the listview in the onActivityCreated in the Fragment:
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
getListView().setItemChecked(position, true);
return true;
}});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
((DocumentsActivity)getActivity()).getMenuInflater().inflate(R.menu.documents_context_menu, menu);
return true;
}
});
super.onActivityCreated(savedInstanceState);
}
When I long click on a listitem the action mode gets started and the menu documents_context_menu appears to be the action bar. But the problem is, the action bar appears above the toolbar and the toolbar won't disappear (see the picture).

I've tried to call getSupportActionBar().hide() or set it to null or even use another style/theme. It all didn't work. Sometimes the blue toolbar was completely white, but that is all.
I have absolutely no idea why the toolbar won't disappear. May you give some advice?
Thanks in advance!
_____ Update 1 _____
This is the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:fitsSystemWindows">true</item>
<item name="colorAccent">@color/darkblue100</item>
<item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="android:actionMenuTextColor">@color/black</item>
</style>
And this is how the action bar is set in the Activity:
protected void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
requestWindowFeature(5);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents);
Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
args = getIntent().getExtras();
if (findViewById(R.id.container_documents) != null && savedInstanceState == null) {
showDocumentsFragment();
}
}
add a comment |
I am trying to implement the feature that, when I am long clicking a list item, the action mode shall start and it shall be possible to delete one or more items.
I am starting in the MainActivity DocumentsActivity a search, which starts a Fragment DocumentsFragment with the ListView and their items. The ListAdapter is initialized and set via method call setListAdapter(this.documentsAdapter) in the onCreate in the Fragment. I set various listeners on the listview in the onActivityCreated in the Fragment:
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
getListView().setItemChecked(position, true);
return true;
}});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
((DocumentsActivity)getActivity()).getMenuInflater().inflate(R.menu.documents_context_menu, menu);
return true;
}
});
super.onActivityCreated(savedInstanceState);
}
When I long click on a listitem the action mode gets started and the menu documents_context_menu appears to be the action bar. But the problem is, the action bar appears above the toolbar and the toolbar won't disappear (see the picture).

I've tried to call getSupportActionBar().hide() or set it to null or even use another style/theme. It all didn't work. Sometimes the blue toolbar was completely white, but that is all.
I have absolutely no idea why the toolbar won't disappear. May you give some advice?
Thanks in advance!
_____ Update 1 _____
This is the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:fitsSystemWindows">true</item>
<item name="colorAccent">@color/darkblue100</item>
<item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="android:actionMenuTextColor">@color/black</item>
</style>
And this is how the action bar is set in the Activity:
protected void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
requestWindowFeature(5);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents);
Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
args = getIntent().getExtras();
if (findViewById(R.id.container_documents) != null && savedInstanceState == null) {
showDocumentsFragment();
}
}
1
Can you please post your Apptheme code fromstyles.xmlandonCreatewhere yousetSupportActionBar?
– Mayur Gajra
Jan 14 at 14:58
Possible duplicate of Toolbar and Contextual ActionBar with AppCompat-v7
– Ali Ahsan
Jan 14 at 15:00
@MayurGajra I added the sources
– Marcel Hofgesang
Jan 14 at 15:19
can you share your Manifest file?
– Abhinav Gupta
Jan 17 at 10:49
@AbhinavGupta there it is
– Marcel Hofgesang
Jan 17 at 11:24
add a comment |
I am trying to implement the feature that, when I am long clicking a list item, the action mode shall start and it shall be possible to delete one or more items.
I am starting in the MainActivity DocumentsActivity a search, which starts a Fragment DocumentsFragment with the ListView and their items. The ListAdapter is initialized and set via method call setListAdapter(this.documentsAdapter) in the onCreate in the Fragment. I set various listeners on the listview in the onActivityCreated in the Fragment:
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
getListView().setItemChecked(position, true);
return true;
}});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
((DocumentsActivity)getActivity()).getMenuInflater().inflate(R.menu.documents_context_menu, menu);
return true;
}
});
super.onActivityCreated(savedInstanceState);
}
When I long click on a listitem the action mode gets started and the menu documents_context_menu appears to be the action bar. But the problem is, the action bar appears above the toolbar and the toolbar won't disappear (see the picture).

I've tried to call getSupportActionBar().hide() or set it to null or even use another style/theme. It all didn't work. Sometimes the blue toolbar was completely white, but that is all.
I have absolutely no idea why the toolbar won't disappear. May you give some advice?
Thanks in advance!
_____ Update 1 _____
This is the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:fitsSystemWindows">true</item>
<item name="colorAccent">@color/darkblue100</item>
<item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="android:actionMenuTextColor">@color/black</item>
</style>
And this is how the action bar is set in the Activity:
protected void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
requestWindowFeature(5);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents);
Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
args = getIntent().getExtras();
if (findViewById(R.id.container_documents) != null && savedInstanceState == null) {
showDocumentsFragment();
}
}
I am trying to implement the feature that, when I am long clicking a list item, the action mode shall start and it shall be possible to delete one or more items.
I am starting in the MainActivity DocumentsActivity a search, which starts a Fragment DocumentsFragment with the ListView and their items. The ListAdapter is initialized and set via method call setListAdapter(this.documentsAdapter) in the onCreate in the Fragment. I set various listeners on the listview in the onActivityCreated in the Fragment:
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
getListView().setItemChecked(position, true);
return true;
}});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
((DocumentsActivity)getActivity()).getMenuInflater().inflate(R.menu.documents_context_menu, menu);
return true;
}
});
super.onActivityCreated(savedInstanceState);
}
When I long click on a listitem the action mode gets started and the menu documents_context_menu appears to be the action bar. But the problem is, the action bar appears above the toolbar and the toolbar won't disappear (see the picture).

I've tried to call getSupportActionBar().hide() or set it to null or even use another style/theme. It all didn't work. Sometimes the blue toolbar was completely white, but that is all.
I have absolutely no idea why the toolbar won't disappear. May you give some advice?
Thanks in advance!
_____ Update 1 _____
This is the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:fitsSystemWindows">true</item>
<item name="colorAccent">@color/darkblue100</item>
<item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
<item name="android:actionMenuTextColor">@color/black</item>
</style>
And this is how the action bar is set in the Activity:
protected void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
requestWindowFeature(5);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents);
Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
args = getIntent().getExtras();
if (findViewById(R.id.container_documents) != null && savedInstanceState == null) {
showDocumentsFragment();
}
}
edited Jan 25 at 8:02
Marcel Hofgesang
asked Jan 14 at 14:43
Marcel HofgesangMarcel Hofgesang
91319
91319
1
Can you please post your Apptheme code fromstyles.xmlandonCreatewhere yousetSupportActionBar?
– Mayur Gajra
Jan 14 at 14:58
Possible duplicate of Toolbar and Contextual ActionBar with AppCompat-v7
– Ali Ahsan
Jan 14 at 15:00
@MayurGajra I added the sources
– Marcel Hofgesang
Jan 14 at 15:19
can you share your Manifest file?
– Abhinav Gupta
Jan 17 at 10:49
@AbhinavGupta there it is
– Marcel Hofgesang
Jan 17 at 11:24
add a comment |
1
Can you please post your Apptheme code fromstyles.xmlandonCreatewhere yousetSupportActionBar?
– Mayur Gajra
Jan 14 at 14:58
Possible duplicate of Toolbar and Contextual ActionBar with AppCompat-v7
– Ali Ahsan
Jan 14 at 15:00
@MayurGajra I added the sources
– Marcel Hofgesang
Jan 14 at 15:19
can you share your Manifest file?
– Abhinav Gupta
Jan 17 at 10:49
@AbhinavGupta there it is
– Marcel Hofgesang
Jan 17 at 11:24
1
1
Can you please post your Apptheme code from
styles.xml and onCreate where you setSupportActionBar ?– Mayur Gajra
Jan 14 at 14:58
Can you please post your Apptheme code from
styles.xml and onCreate where you setSupportActionBar ?– Mayur Gajra
Jan 14 at 14:58
Possible duplicate of Toolbar and Contextual ActionBar with AppCompat-v7
– Ali Ahsan
Jan 14 at 15:00
Possible duplicate of Toolbar and Contextual ActionBar with AppCompat-v7
– Ali Ahsan
Jan 14 at 15:00
@MayurGajra I added the sources
– Marcel Hofgesang
Jan 14 at 15:19
@MayurGajra I added the sources
– Marcel Hofgesang
Jan 14 at 15:19
can you share your Manifest file?
– Abhinav Gupta
Jan 17 at 10:49
can you share your Manifest file?
– Abhinav Gupta
Jan 17 at 10:49
@AbhinavGupta there it is
– Marcel Hofgesang
Jan 17 at 11:24
@AbhinavGupta there it is
– Marcel Hofgesang
Jan 17 at 11:24
add a comment |
5 Answers
5
active
oldest
votes
all of this answers are fine you should try them but what you need is a ContextualMenu so you should first add views to a registerForContextMenu() so menu knows which menus are contextual then implement the onCreateContextMenu of your Activity
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
then implement onContextItemSelected() like this :
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then you have to perform an action on views and implement AbsListView.MultiChoiceModeListener then call setChoiceMode() with the CHOICE_MODE_MULTIPLE_MODAL argument. like this :
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an <code><a href="/reference/android /view/ActionMode.html#invalidate()">invalidate()</a></code> request
return false;
}
});
all of what i said and ultimately more, you can find in this android developer documentation
add a comment |
Well as far as i go through your code and understand it, you have done everything that you provided your Toolbar to act as ActionBar and used .NoActionBar theme except according to Android Developer you should also set the windowActionBar attribute to false in your style. enter link description here Second para clears it out.
I hope it helps!
add a comment |
As pointed out in the link provided in comments you just need to add following line to your AppTheme style:
<item name="windowActionModeOverlay">true</item>
It just indicates that action mode should overlay window content instead of pushing it down,it tells that you don't need any reserved space for action mode.
Sadly this doesn't work for me..
– Marcel Hofgesang
Jan 14 at 15:23
@MarcelHofgesang How are you setting action mode then? Have tried setting acion mode on toolbar instead of activity like this :toolbar.startActionMode(callback);?
– Mayur Gajra
Jan 14 at 15:36
yes I've tried that way too but it had some more bugs... Actually the action mode gets activated when theMultiChoiceModeListenergets called. See the description on android developers site developer.android.com/reference/android/widget/…
– Marcel Hofgesang
Jan 14 at 15:46
add a comment |
Add:
//Set action mode null after use
public void setNullToActionMode() {
if (mActionMode != null)
mActionMode = null;
}
Or:
//Remove selected selections
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
}
This doesn't refer on the problem that I state. The removing of an list item is not the problem, just the 2nd toolbar. Also I those methods aren't applicable in my scenario.
– Marcel Hofgesang
Jan 18 at 12:40
add a comment |
Actually the was caused by different settings.
First, as multiple times said, the windowActionBar was set true, also was the attribute fitsSystemWindows.I deleted both lines in styles.xml.
Then there was in the activity layout the attribute layout_marginTop="?actionBarSize" I didn't see, following in complete confusion. But this attribute needs to be there so I am handling it in the method onActivityCreated when onCreateActionMode and onDestroyActionMode are called.
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {...}
});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
LinearLayout ll = ((DocumentsActivity)getActivity()).findViewById(R.id.container_documents);
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, 0, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().hide();
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.documents_context_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, R.attr.actionBarSize, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().show();
}
});
super.onActivityCreated(savedInstanceState);
}
Currently the main problem is solved, but more problems appeared.
I keep you up to date!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54183643%2ftoolbar-wont-disappear-in-action-mode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
all of this answers are fine you should try them but what you need is a ContextualMenu so you should first add views to a registerForContextMenu() so menu knows which menus are contextual then implement the onCreateContextMenu of your Activity
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
then implement onContextItemSelected() like this :
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then you have to perform an action on views and implement AbsListView.MultiChoiceModeListener then call setChoiceMode() with the CHOICE_MODE_MULTIPLE_MODAL argument. like this :
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an <code><a href="/reference/android /view/ActionMode.html#invalidate()">invalidate()</a></code> request
return false;
}
});
all of what i said and ultimately more, you can find in this android developer documentation
add a comment |
all of this answers are fine you should try them but what you need is a ContextualMenu so you should first add views to a registerForContextMenu() so menu knows which menus are contextual then implement the onCreateContextMenu of your Activity
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
then implement onContextItemSelected() like this :
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then you have to perform an action on views and implement AbsListView.MultiChoiceModeListener then call setChoiceMode() with the CHOICE_MODE_MULTIPLE_MODAL argument. like this :
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an <code><a href="/reference/android /view/ActionMode.html#invalidate()">invalidate()</a></code> request
return false;
}
});
all of what i said and ultimately more, you can find in this android developer documentation
add a comment |
all of this answers are fine you should try them but what you need is a ContextualMenu so you should first add views to a registerForContextMenu() so menu knows which menus are contextual then implement the onCreateContextMenu of your Activity
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
then implement onContextItemSelected() like this :
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then you have to perform an action on views and implement AbsListView.MultiChoiceModeListener then call setChoiceMode() with the CHOICE_MODE_MULTIPLE_MODAL argument. like this :
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an <code><a href="/reference/android /view/ActionMode.html#invalidate()">invalidate()</a></code> request
return false;
}
});
all of what i said and ultimately more, you can find in this android developer documentation
all of this answers are fine you should try them but what you need is a ContextualMenu so you should first add views to a registerForContextMenu() so menu knows which menus are contextual then implement the onCreateContextMenu of your Activity
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
then implement onContextItemSelected() like this :
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then you have to perform an action on views and implement AbsListView.MultiChoiceModeListener then call setChoiceMode() with the CHOICE_MODE_MULTIPLE_MODAL argument. like this :
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an <code><a href="/reference/android /view/ActionMode.html#invalidate()">invalidate()</a></code> request
return false;
}
});
all of what i said and ultimately more, you can find in this android developer documentation
answered Jan 22 at 12:40
pouyapouya
6132926
6132926
add a comment |
add a comment |
Well as far as i go through your code and understand it, you have done everything that you provided your Toolbar to act as ActionBar and used .NoActionBar theme except according to Android Developer you should also set the windowActionBar attribute to false in your style. enter link description here Second para clears it out.
I hope it helps!
add a comment |
Well as far as i go through your code and understand it, you have done everything that you provided your Toolbar to act as ActionBar and used .NoActionBar theme except according to Android Developer you should also set the windowActionBar attribute to false in your style. enter link description here Second para clears it out.
I hope it helps!
add a comment |
Well as far as i go through your code and understand it, you have done everything that you provided your Toolbar to act as ActionBar and used .NoActionBar theme except according to Android Developer you should also set the windowActionBar attribute to false in your style. enter link description here Second para clears it out.
I hope it helps!
Well as far as i go through your code and understand it, you have done everything that you provided your Toolbar to act as ActionBar and used .NoActionBar theme except according to Android Developer you should also set the windowActionBar attribute to false in your style. enter link description here Second para clears it out.
I hope it helps!
answered Jan 21 at 22:01
Vanshaj DagaVanshaj Daga
1695
1695
add a comment |
add a comment |
As pointed out in the link provided in comments you just need to add following line to your AppTheme style:
<item name="windowActionModeOverlay">true</item>
It just indicates that action mode should overlay window content instead of pushing it down,it tells that you don't need any reserved space for action mode.
Sadly this doesn't work for me..
– Marcel Hofgesang
Jan 14 at 15:23
@MarcelHofgesang How are you setting action mode then? Have tried setting acion mode on toolbar instead of activity like this :toolbar.startActionMode(callback);?
– Mayur Gajra
Jan 14 at 15:36
yes I've tried that way too but it had some more bugs... Actually the action mode gets activated when theMultiChoiceModeListenergets called. See the description on android developers site developer.android.com/reference/android/widget/…
– Marcel Hofgesang
Jan 14 at 15:46
add a comment |
As pointed out in the link provided in comments you just need to add following line to your AppTheme style:
<item name="windowActionModeOverlay">true</item>
It just indicates that action mode should overlay window content instead of pushing it down,it tells that you don't need any reserved space for action mode.
Sadly this doesn't work for me..
– Marcel Hofgesang
Jan 14 at 15:23
@MarcelHofgesang How are you setting action mode then? Have tried setting acion mode on toolbar instead of activity like this :toolbar.startActionMode(callback);?
– Mayur Gajra
Jan 14 at 15:36
yes I've tried that way too but it had some more bugs... Actually the action mode gets activated when theMultiChoiceModeListenergets called. See the description on android developers site developer.android.com/reference/android/widget/…
– Marcel Hofgesang
Jan 14 at 15:46
add a comment |
As pointed out in the link provided in comments you just need to add following line to your AppTheme style:
<item name="windowActionModeOverlay">true</item>
It just indicates that action mode should overlay window content instead of pushing it down,it tells that you don't need any reserved space for action mode.
As pointed out in the link provided in comments you just need to add following line to your AppTheme style:
<item name="windowActionModeOverlay">true</item>
It just indicates that action mode should overlay window content instead of pushing it down,it tells that you don't need any reserved space for action mode.
answered Jan 14 at 15:19
Mayur GajraMayur Gajra
391119
391119
Sadly this doesn't work for me..
– Marcel Hofgesang
Jan 14 at 15:23
@MarcelHofgesang How are you setting action mode then? Have tried setting acion mode on toolbar instead of activity like this :toolbar.startActionMode(callback);?
– Mayur Gajra
Jan 14 at 15:36
yes I've tried that way too but it had some more bugs... Actually the action mode gets activated when theMultiChoiceModeListenergets called. See the description on android developers site developer.android.com/reference/android/widget/…
– Marcel Hofgesang
Jan 14 at 15:46
add a comment |
Sadly this doesn't work for me..
– Marcel Hofgesang
Jan 14 at 15:23
@MarcelHofgesang How are you setting action mode then? Have tried setting acion mode on toolbar instead of activity like this :toolbar.startActionMode(callback);?
– Mayur Gajra
Jan 14 at 15:36
yes I've tried that way too but it had some more bugs... Actually the action mode gets activated when theMultiChoiceModeListenergets called. See the description on android developers site developer.android.com/reference/android/widget/…
– Marcel Hofgesang
Jan 14 at 15:46
Sadly this doesn't work for me..
– Marcel Hofgesang
Jan 14 at 15:23
Sadly this doesn't work for me..
– Marcel Hofgesang
Jan 14 at 15:23
@MarcelHofgesang How are you setting action mode then? Have tried setting acion mode on toolbar instead of activity like this :
toolbar.startActionMode(callback); ?– Mayur Gajra
Jan 14 at 15:36
@MarcelHofgesang How are you setting action mode then? Have tried setting acion mode on toolbar instead of activity like this :
toolbar.startActionMode(callback); ?– Mayur Gajra
Jan 14 at 15:36
yes I've tried that way too but it had some more bugs... Actually the action mode gets activated when the
MultiChoiceModeListener gets called. See the description on android developers site developer.android.com/reference/android/widget/…– Marcel Hofgesang
Jan 14 at 15:46
yes I've tried that way too but it had some more bugs... Actually the action mode gets activated when the
MultiChoiceModeListener gets called. See the description on android developers site developer.android.com/reference/android/widget/…– Marcel Hofgesang
Jan 14 at 15:46
add a comment |
Add:
//Set action mode null after use
public void setNullToActionMode() {
if (mActionMode != null)
mActionMode = null;
}
Or:
//Remove selected selections
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
}
This doesn't refer on the problem that I state. The removing of an list item is not the problem, just the 2nd toolbar. Also I those methods aren't applicable in my scenario.
– Marcel Hofgesang
Jan 18 at 12:40
add a comment |
Add:
//Set action mode null after use
public void setNullToActionMode() {
if (mActionMode != null)
mActionMode = null;
}
Or:
//Remove selected selections
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
}
This doesn't refer on the problem that I state. The removing of an list item is not the problem, just the 2nd toolbar. Also I those methods aren't applicable in my scenario.
– Marcel Hofgesang
Jan 18 at 12:40
add a comment |
Add:
//Set action mode null after use
public void setNullToActionMode() {
if (mActionMode != null)
mActionMode = null;
}
Or:
//Remove selected selections
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
}
Add:
//Set action mode null after use
public void setNullToActionMode() {
if (mActionMode != null)
mActionMode = null;
}
Or:
//Remove selected selections
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
}
edited Jan 19 at 10:12
Zoe
11.6k74379
11.6k74379
answered Jan 17 at 14:23
cod-e-rectioncod-e-rection
269314
269314
This doesn't refer on the problem that I state. The removing of an list item is not the problem, just the 2nd toolbar. Also I those methods aren't applicable in my scenario.
– Marcel Hofgesang
Jan 18 at 12:40
add a comment |
This doesn't refer on the problem that I state. The removing of an list item is not the problem, just the 2nd toolbar. Also I those methods aren't applicable in my scenario.
– Marcel Hofgesang
Jan 18 at 12:40
This doesn't refer on the problem that I state. The removing of an list item is not the problem, just the 2nd toolbar. Also I those methods aren't applicable in my scenario.
– Marcel Hofgesang
Jan 18 at 12:40
This doesn't refer on the problem that I state. The removing of an list item is not the problem, just the 2nd toolbar. Also I those methods aren't applicable in my scenario.
– Marcel Hofgesang
Jan 18 at 12:40
add a comment |
Actually the was caused by different settings.
First, as multiple times said, the windowActionBar was set true, also was the attribute fitsSystemWindows.I deleted both lines in styles.xml.
Then there was in the activity layout the attribute layout_marginTop="?actionBarSize" I didn't see, following in complete confusion. But this attribute needs to be there so I am handling it in the method onActivityCreated when onCreateActionMode and onDestroyActionMode are called.
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {...}
});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
LinearLayout ll = ((DocumentsActivity)getActivity()).findViewById(R.id.container_documents);
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, 0, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().hide();
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.documents_context_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, R.attr.actionBarSize, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().show();
}
});
super.onActivityCreated(savedInstanceState);
}
Currently the main problem is solved, but more problems appeared.
I keep you up to date!
add a comment |
Actually the was caused by different settings.
First, as multiple times said, the windowActionBar was set true, also was the attribute fitsSystemWindows.I deleted both lines in styles.xml.
Then there was in the activity layout the attribute layout_marginTop="?actionBarSize" I didn't see, following in complete confusion. But this attribute needs to be there so I am handling it in the method onActivityCreated when onCreateActionMode and onDestroyActionMode are called.
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {...}
});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
LinearLayout ll = ((DocumentsActivity)getActivity()).findViewById(R.id.container_documents);
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, 0, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().hide();
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.documents_context_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, R.attr.actionBarSize, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().show();
}
});
super.onActivityCreated(savedInstanceState);
}
Currently the main problem is solved, but more problems appeared.
I keep you up to date!
add a comment |
Actually the was caused by different settings.
First, as multiple times said, the windowActionBar was set true, also was the attribute fitsSystemWindows.I deleted both lines in styles.xml.
Then there was in the activity layout the attribute layout_marginTop="?actionBarSize" I didn't see, following in complete confusion. But this attribute needs to be there so I am handling it in the method onActivityCreated when onCreateActionMode and onDestroyActionMode are called.
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {...}
});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
LinearLayout ll = ((DocumentsActivity)getActivity()).findViewById(R.id.container_documents);
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, 0, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().hide();
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.documents_context_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, R.attr.actionBarSize, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().show();
}
});
super.onActivityCreated(savedInstanceState);
}
Currently the main problem is solved, but more problems appeared.
I keep you up to date!
Actually the was caused by different settings.
First, as multiple times said, the windowActionBar was set true, also was the attribute fitsSystemWindows.I deleted both lines in styles.xml.
Then there was in the activity layout the attribute layout_marginTop="?actionBarSize" I didn't see, following in complete confusion. But this attribute needs to be there so I am handling it in the method onActivityCreated when onCreateActionMode and onDestroyActionMode are called.
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {...}
});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
LinearLayout ll = ((DocumentsActivity)getActivity()).findViewById(R.id.container_documents);
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, 0, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().hide();
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.documents_context_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) ll.getLayoutParams();
params.setMargins(0, R.attr.actionBarSize, 0, 0);
ll.setLayoutParams(params);
((DocumentsActivity)getActivity()).getSupportActionBar().show();
}
});
super.onActivityCreated(savedInstanceState);
}
Currently the main problem is solved, but more problems appeared.
I keep you up to date!
answered Jan 25 at 8:37
Marcel HofgesangMarcel Hofgesang
91319
91319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54183643%2ftoolbar-wont-disappear-in-action-mode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you please post your Apptheme code from
styles.xmlandonCreatewhere yousetSupportActionBar?– Mayur Gajra
Jan 14 at 14:58
Possible duplicate of Toolbar and Contextual ActionBar with AppCompat-v7
– Ali Ahsan
Jan 14 at 15:00
@MayurGajra I added the sources
– Marcel Hofgesang
Jan 14 at 15:19
can you share your Manifest file?
– Abhinav Gupta
Jan 17 at 10:49
@AbhinavGupta there it is
– Marcel Hofgesang
Jan 17 at 11:24