Example usage for android.view ActionMode setCustomView

List of usage examples for android.view ActionMode setCustomView

Introduction

In this page you can find the example usage for android.view ActionMode setCustomView.

Prototype

public abstract void setCustomView(View view);

Source Link

Document

Set a custom view for this action mode.

Usage

From source file:com.nearnotes.NoteList.java

@Override
public void onStart() {
    super.onStart();

    mCallback.setActionItems(NOTE_LIST);
    mSelectedIds = new ArrayList<Long>();
    mDbHelper = new NotesDbAdapter(getActivity()); // Create new custom database class for sqlite and pass the current context as a variable
    mDbHelper.open(); // Gets the writable database

    Bundle bundle = getArguments();/*ww  w. j  a v  a  2s. co m*/
    mLongitude = bundle.getDouble("longitude");
    mLatitude = bundle.getDouble("latitude");
    fillData(mLongitude, mLatitude);
    mListView = getListView();

    getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

    getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {

        private int nr = 0;

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionModeFlag = false;
            Log.e("onDestroyActionMode", "onDestroyActionMode");
            mAdapter.clearSelection();
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mActionModeFlag = true;

            nr = 0;
            MenuInflater inflater = getActivity().getMenuInflater();
            inflater.inflate(R.menu.contextual_menu, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {

            case R.id.context_delete:
                for (Long s : mSelectedIds) {
                    mDbHelper.deleteNote(s);
                    if (mDbHelper.fetchSetting() == s) {
                        mDbHelper.removeSetting();
                    }
                }
                nr = 0;
                mAdapter.clearSelection();
                fillData(mLongitude, mLatitude);
                mode.finish();

                return true;
            case R.id.context_select_all:
                SparseBooleanArray checkSparse = getListView().getCheckedItemPositions();
                for (int i = 0; i < mListView.getCount(); i++) {
                    if (!checkSparse.get(i)) {
                        getListView().setItemChecked(i, true);

                    }
                }
                return true;
            }
            return false;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            Log.e("checked", String.valueOf(checked) + " id: " + String.valueOf(id) + " position: "
                    + String.valueOf(position));
            if (checked) {
                nr++;
                mSelectedIds.add(id);
                mAdapter.setNewSelection(position, checked);
            } else {
                mSelectedIds.remove(id);
                nr--;
                mAdapter.removeSelection(position);
            }

            TextView tv = (TextView) getActivity().getLayoutInflater().inflate(R.layout.contextual_title, null);
            tv.setText(nr + " selected");
            mode.setCustomView(tv);

        }
    });

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
            return false;
        }
    });

}