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();
}
}
all of this answers are fine you should try them but what you need is a
ContextualMenu
so you should first add views to aregisterForContextMenu()
so menu knows which menus are contextual then implement theonCreateContextMenu
of yourActivity
then implement
onContextItemSelected()
like this :then you have to perform an action on views and implement
AbsListView.MultiChoiceModeListener
then callsetChoiceMode()
with theCHOICE_MODE_MULTIPLE_MODAL
argument. like this :all of what i said and ultimately more, you can find in this android developer documentation
As pointed out in the link provided in comments you just need to add following line to your AppTheme style:
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.
Actually the was caused by different settings. First, as multiple times said, the
windowActionBar
was settrue
, also was the attributefitsSystemWindows.
I deleted both lines instyles.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 methodonActivityCreated
whenonCreateActionMode
andonDestroyActionMode
are called.After that all I had the automagically problem that the listview items disappeared. I fixed it by commiting my fragment again in
onDestroyActionMode
.Add:
Or:
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!