Skip to content

Instantly share code, notes, and snippets.

@dalewking
Created June 6, 2014 16:11
Show Gist options
  • Save dalewking/b087aacaaa495bc434a7 to your computer and use it in GitHub Desktop.
Save dalewking/b087aacaaa495bc434a7 to your computer and use it in GitHub Desktop.
Backwards compatible Contextual Action Mode
import android.content.Context;
import android.support.v7.view.ActionMode;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
public class CamCompatListView extends ListView
{
public CamCompatListView(Context context)
{
super(context);
}
public CamCompatListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CamCompatListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
private ActionMode actionMode;
public void setActionMode(ActionMode mode)
{
actionMode = mode;
}
@Override
public boolean performItemClick(View view, int position, long id)
{
if (actionMode == null)
{
setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
boolean returnValue = super.performItemClick(view, position, id);
if (actionMode != null)
{
actionMode.invalidate();
}
return returnValue;
}
}
import android.annotation.SuppressLint;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.view.ActionMode;
import android.util.SparseBooleanArray;
import android.view.HapticFeedbackConstants;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
public class MyActivity extends ActionBarActivity
implements ActionMode.Callback, AdapterView.OnItemLongClickListener,
AdapterView.OnItemClickListener
{
ActionMode actionMode = null;
CamCompatListView listView;
@SuppressLint("NewApi")
public void initialize(CamCompatListView listView)
{
listView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
listView.setOnItemLongClickListener(this);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (actionMode == null)
{
// Code you want on a normal click of an item
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
int itemId = item.getItemId();
if (itemId == R.id.myAction)
{
SparseBooleanArray checked = listView.getCheckedItemPositions();
if (checked != null)
{
for (int i = 0 ; i < checked.size(); i++)
{
if(checked.valueAt(i))
{
// Perform action on the item
}
}
}
mode.finish();
return true;
}
return false;
}
@SuppressLint("NewApi")
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
if (actionMode == null)
{
// This needs to be done early, because it will clear selections on the list
startSupportActionMode(this);
}
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(position, !listView.isItemChecked(position));
listView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
actionMode.invalidate();
return true;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
mode.getMenuInflater().inflate(R.menu.my_context_menu, menu);
actionMode = mode;
listView.setActionMode(mode);
return true;
}
@SuppressLint("NewApi")
@Override
public void onDestroyActionMode(ActionMode mode)
{
SparseBooleanArray checked = listView.getCheckedItemPositions();
if (checked != null)
{
for(int i = checked.indexOfValue(true); i >= 0; i = checked.indexOfValue(true))
{
listView.setItemChecked(checked.keyAt(i), false);
}
}
listView.setActionMode(null);
actionMode = null;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
SparseBooleanArray checked = listView.getCheckedItemPositions();
int numberChecked = 0;
if (checked != null)
{
for (int i = 0 ; i < checked.size(); i++)
{
if(checked.valueAt(i))
{
numberChecked++;
}
}
}
if (numberChecked > 0)
{
mode.setTitle(getResources().getQuantityString(R.plurals.itemsSelected, numberChecked, numberChecked));
}
else
{
mode.finish();
}
return true;
}
}
@dalewking
Copy link
Author

ActionBarCompat allows us to use action bars in older versions of Android, however due to differences in ListView Contextual Action Mode does not work out of the box. Here is some code I used to get the correct behavior.

@rubensedanoc
Copy link

Excelent, I just implemented and Its working great in gingerbread. Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment