Contextual Menu in a Fragment inside a Fragment (ActionMode) - android

I got a Fragment B in a Fragment A in a Activity. Works as expected.
When clicking an item in Fragment B, I want to display a contextual menu bar.
I am working with ActionbarSherlock.
What i've done this inside my Fragment B:
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.entry_list_context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// make sure no item is selected when bar is shown
adapter.clearSelection();
adapter.notifyDataSetChanged();
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.d("EntryList", "Item '" + item.getTitle()
+ "' clicked [onActionItemClicked()]");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
adapter.clearSelection();
adapter.notifyDataSetChanged();
contextualMode = null;
}
};
private ActionMode contextualMode;
#Override
public void onItemClick(AdapterView<?> parentView, View itemView,
int index, long id) {
DocumentEntity entry = (DocumentEntity) itemView.getTag();
// something went wrong
if (entry == null) {
Log.e("EntryList", "Tag-Less item clicked [onItemClick()]");
return;
}
if (contextualMode != null) {
Log.d("EntryList",
"contextualMode is not yet initialized [onItemClick()]");
contextualMode = getSherlockActivity().startActionMode(
mActionModeCallback);
} else {
Log.d("EntryList",
"contextualMode already initialized [onItemClick()]");
}
entry.setSelected(!entry.isSelected());
Log.d("EntryList", "entry.selected set to " + entry.isSelected()
+ " [onItemClick()]");
}
The selection works pretty good, but no contextual Actionbar is shown.
The debug result is:
contextualMode already initialized [onItemClick()] entry.selected set
to 'true' [onItemClick()]
There is no other position where contextualMode is set...

I got a Fragment B in a Fragment A...
Android does not support embedding a fragment within another fragment. Sorry. This leads me to believe that your problem goes beyond the fact that your contextual ActionBar is not being shown. I suggest you clarify your original post.

Related

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 DocumentsActivity a search, which starts a Fragment DocumentsFragment with a ListView and their items. The ListAdapter is initialized and set via method call setListAdapter(this.documentsAdapter) in onCreate of 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 long clicking 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();
}
}
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.
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!
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>invalidate()</code> request
return false;
}
});
all of what i said and ultimately more, you can find in this android developer documentation
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();
}
Actually the issue were caused by different things.
First, 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 did not 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.
After that all I had the automagically problem that the listview items disappeared. I fixed it by commiting my fragment again in onDestroyActionMode.
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();
if (getFragmentManager() != null)
getFragmentManager()
.beginTransaction()
.detach(this)
.attach(this)
.commit();
}
});
super.onActivityCreated(savedInstanceState);
}
In Kotlin, assuming that your Activity extends from AppCompatActivity
You should get the instance of the support action bar and call the methods
supportActionBar?.show() or supportActionBar?.hide()
If you're using a fragment, you can access the action bar from the Fragment's activity instance
(activity as AppCompatActivity).supportActionBar?.show()

setBackgroundResource in onItemCheckStateChanged doesn't work for the first selection

I am working on an android project where I have a grid view and I am setting it up with a MultiChoiceModeListener.
When I long click, it successfully creates the contextual action bar but setting the setBackgroundResource on the initial item that creates the CAB doesn't have any affect, however all subsequent items that are selected, the background resource is successfully applied. Below is the code I am using:
gridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
gridView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
{
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
Log.d("DirectoryPicker-CAB", "CheckedStateChanged");
//findViewById((int)gridView.getItemIdAtPosition(position)).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, R.color.appPrimaryColour));
//Toast.makeText(DirectoryPicker.this, "Item CAB Clicked: Checked: " + (checked ? "true" : "false"), Toast.LENGTH_LONG).show();
Toast.makeText(DirectoryPicker.this, "Position: " + position, Toast.LENGTH_LONG).show();
if (checked)
{
//gridView.getChildAt(position).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, R.color.appPrimaryColour));
Log.d("DirectoryPicker-View", gridView.getChildAt(position).toString());
gridView.getChildAt(position).setBackgroundResource(R.color.appPrimaryColour);
mode.invalidate();
directoryAdapter.getItem(position).setSelected(true);
selectedDirectories.add(directoryAdapter.getItem(position));
}
else
{
directoryAdapter.getItem(position).setSelected(false);
FileManager.DirectoryOrFileInfo selectedDirectory = directoryAdapter.getItem(position);
for (int i = 0; i < selectedDirectories.size(); i++)
{
if (selectedDirectories.get(i) == selectedDirectory)
{
selectedDirectories.remove(i);
selectedDirectory.setSelected(false);
}
}
gridView.getChildAt(position).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, android.R.color.transparent));
}
mode.setTitle(selectedDirectories.size() + " selected");
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.file_dir_cab_menu, menu);
Log.d("DirectoryPicker-CAB", "OnActionModeCreate");
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode)
{
selectedDirectories.clear();
}
});
Why does the background resource not get successfully applied for the first item that is long clicked that creates the CAB, but it works fine for all subsequent selections.
I've figured why this isn't working now. When the CAB is created, it triggers the getView function to be called again in my adapter so it ends up resetting the view back to how it was again.
I've therefore changed my adapter instead in the getView method so it checks whether the item is marked as checked, and it sets the background resource.

Android CAB destroying after listview notifydatasetchanged

I am using setMultiChoiceModeListener on Listview to invoke Action Mode for selecting multiple items to be deleted. On ListView scroll I load more data from server and notify the adapter. After adapter has been notified, the Action Mode (if invoked) is destroyed and recreated, which makes the title and selected arraylist empty (You can see the picture below). While the listview item selection remains. I want the CAB to be persistent like we see it in Gmail app, where it does not destroy on loading more data.
Below is the code for Action Mode
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Item mItem = adapter.items.get(position);
if (mItem.isSection()) {
} else {
mode.setTitle(listView.getCheckedItemCount() + " Selected");
EntryItem mEntryItem = (EntryItem) mItem;
orderid = mEntryItem.orderId;
if (checked) {
selectedIdList.add(orderid);
} else {
selectedIdList.remove(orderid);
}
// Toggle the state of item after every click on it
adapter.toggleSelection(position);
}
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.delete){
if (selectedIdList.size() > 0) {
deleteItems(selectedIdList);
}
mode.finish();
return true;
}
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_main, menu);
actionMode = mode;
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
if (selectedIdList.size() > 0) {
selectedIdList.clear();
adapter.mSelectedItemsIds.clear();
}
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});
Any help will be highly appreciated, thanks in advance.
You need to save selected items id in order to retain state and show as selected after you refresh/notify adapter changes like shown in below examples:
1) https://androidperspective.wordpress.com/2013/04/17/contextual-action-bar-with-listview/
(It shows selection and saving selected item ids in array to save state).
2) http://theopentutorials.com/examples/android/listview/android-contextual-action-bar-for-listview-item-deletion-using-actionbarsherlock/
(It shows complete example you want for deletion just using sherlock action bar it would be little change only).
Glad to know if it helps you.

Android ListView Single Choice Mode with Action Mode

I would like to create an app screen that include one ListView.
When the user performs long press on ListView item, I would like to show action mode. I implemented it with the following code and I got the result which I want.
One problem is that I would like to allow the user to select only one list item and select the context menu item in action mode to do the operation. The following code is allowed the user to select multiple list items.
Is android not support ListView.CHOICE_MODE_SINGLE_MODAL choice mode? I had spend a lot of time in Googling but can't find any reference to implement which allows a single choice mode with context action mode. Please help.
mTestListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
mTestListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean value) {
}
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.context_menu_test_single_choice_mode, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_done:
Toast.makeText(getActivity(), "Done", Toast.LENGTH_SHORT).show();
actionMode.finish();
adapter.notifyDataSetChanged();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
}
Add this code in your method. This code will check item count. If count exceed more than one, it will remove other item except the last one.
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
int selectCount = mList.getCheckedItemCount();
if(selectCount > 1){
SparseBooleanArray checkarr = mList.getCheckedItemPositions();
for(int i=0;i<dataList.size();i++){
/*
check item is checked
and not the last item
* */
if(checkarr.get(i) && position != i){
mList.setItemChecked(i, false);
break;
}
}
}
}
I tried in my app. It works. :). Hope this will help you.

Passing id of ListView item to ActionMode.Callback object

So my problem right now is that right now I am long clicking an item in a ListView which brings up a contextual action bar. The id passed into onItemLongClick is the variable that I would like to use in the mActionModeCallback's on ActionItemClicked() method. This seems like it would be a fairly common procedure since if a user is editing a list of items, you would want to access the id of that row in the database somehow when the user clicked an "edit" or a "delete" action.
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> p, View view, int pos, long id) {
//The id of the row in the database
long variableThatIWantToPassToCallback = id;
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//I would like access to the id of the clicked item here, NOT item.getItemId()
}
public void onDestroyActionMode(ActionMode mode) {}
};
The proper way to do this is to call mActionMode.setTag("1") in onItemCheckedStateChanged and then from the onActionItemClicked function call mode.getTag();
Create your own callback by extending the interface ActionMode.Callback
private interface ActionCallback extends ActionMode.Callback {
public void setClickedView(View view);
}
private ActionCallback mActionModeCallback = new ActionCallback() {
public View mClickedView;
public void setClickedView(View view) {
mClickedView = view;
}
// Called when the action mode is created; startActionMode() was called
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item ) {
switch ( item.getItemId() ) {
case R.id.menu_delete:
Log.v( TAG, "#onActionItemClicked ready to delete the item with id: " + mClickedView.getTag() );
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
} // end switch
}
// Called when the user exits the action mode
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
For a view which has OnLongClickListener attached, override the onLongClick callback this way.
#Override
// Called when the user long-clicks on someView
public boolean onLongClick( View view ) {
// proceed only when actionmode is not null
// otherwise overlapping action modes will be
// displayed
if( mActionMode != null ) {
return false;
}
mActionModeCallback.setClickedView(view);
// Start the CAB using the ActionMode.Callback defined above
mActionMode = startActionMode( mActionModeCallback );
view.setSelected(true);
return true;
}

Categories

Resources