I have a ListView and when the user long-presses on any particular item, the CAB starts.
I am using,
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
and the MultiChoiceModeListener to intercept the callbacks,
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
}
});
Now, in my onResume() (outside this listener), I want to check if my ActionMode is visible/invisible and if it is, then depending upon certain conditions, I want to manually disable/enable it.
How can I do this?
I waited for a day but didn't get any answer on this and solved it myself. I don't know if the approach is perfect.
Declare a field ActionMode
ActionMode mActionMode;
Now in the onCreateActionMode() method you get the ActionMode,
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mActionMode = mode;
}
And in the onDestroyActionMode(),
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
Now anywhere in your code just check if the mActionMode is null or not to check if the ActionMode is enabled or disabled.
Hope this helps. Do post an answer if you have a better solution.
Related
I found a lot of question about this problem, but I can't fix it. I am creating custom contextual action bars (CAB) within WebView i followed this Tutorial
My problem is CAB working fine in setOnLongClickListener but i cant able to select the text.
I referred these links:
Link 1, Link 2
Edit :
mywebview.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mActionMode = MainActivity.this.startActionMode(new ActionBarCallBack());
return true;
}
});
Implement the ActionMode.Callback interface:
class ActionBarCallBack implements ActionMode.Callback {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
}
I Tried like this :
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
mActionMode = Contents.this.startActionMode(new ActionBarCallBack());
}
Help me where i am wrong. Thank in advanced
I use GridView and implement multiple select.
When I select the multiple item from GridView , it will show the menu on the top of screen.
But I don't want the menu show on the top .
I set the GridView to CHOICE_MODE_MULTIPLE_MODAL and implement thr MultiChoiceModeListener of class.
Does there has any method can hide the menu when select the multiple item ?
The code of class is like the following
public class LocalFileBrowserFragment extends Fragment implements MultiChoiceModeListener
The code of setup the GridView is like the following:
private GridView fileListView;
fileListView = (GridView) view.findViewById(R.id.browserList) ;
fileListView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
fileListView.setMultiChoiceModeListener((MultiChoiceModeListener)this);
fileListView.setNumColumns(4);
And the code of MultiChoiceModeListener in GridView is like the following:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
Log.i(TAG, "onCreateActionMode");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
for (int i = 0; i < fileListView.getCount(); i++) {
fileListView.setItemChecked(i, false);
mSelectMap.clear();
}
mFileListAdapter.notifyDataSetChanged();
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
//Log.i(TAG, "onPrepareActionMode");
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
mSelectMap.put(position, checked);
mFileListAdapter.notifyDataSetChanged();
}
I set the GridView to CHOICE_MODE_MULTIPLE_MODAL and implement thr MultiChoiceModeListener of class.
I don't want the menu show on the top.
Does there has any method can hide the menu when select the multiple item ?
I solved the problem, I used the following to show the bar.
bar = getActivity().getActionBar();
bar.show();
Save ActionMode as a field of your MultiChoiceModeListener in onCreateActionMode
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
theActionMode = mode;
return true;
}
Then finish the ActionMode in onItemCheckedStateChanged
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
// Your CheckedChange Code......
theActionMode.finish();
}
I have two textview and I want to implement the textselection feature in both the textview. The Api version is 15 and above. the xml for both the textview is same.
In my code, I have called the method,
tv1.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});
tv2.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});
When I run the code, I am successfully able to select the first textview but not able to select the second textview. Getting the logcat message as Textview is not selectable, action mode is cancelled. I have gone through all the links but not able to resolved the issue.
The xml structure for both the textview is same.
I have also added the:
tv1.setTextIsSelectable(true);
tv2.setTextIsSelectable(true);
In edittext long press created contextmenu. How to remove the contextmenu?
I tried this:
et.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
et.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
return false;
}
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode actionMode,
MenuItem item) {
return false;
}
public void onDestroyActionMode(ActionMode actionMode) {
}
});
But then I can not select text.
I'm not sure that you can remove the menu altogether, but you can call menu.clear() in onPrepareActionMode and this will remove all the menu items from the menu. You'll have to remove the onLongClickListener in order to see the handles again as well.
You can create your own class extending EditText and override the performLongClick() method, that should work as expected
class MyET extends EditText{
public MyET(Context context) {
super(context);
}
#Override
public boolean performLongClick() {
// TODO Auto-generated method stub
return true;
}
}
I've a ListActivity
public class MyActivity extends ListActivity{
protected void onCreate(Bundle savedstate){
super.onCreate(savedstate);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Log.i("xxx", "onPrepareActionMode");
return false;
}
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Log.i("xxx", "pos: "+position+", checked: "+checked);
mode.setTitle(getListView().getCheckedItemCount()+" checked");
}
});
}
}
Here I can see the items getting checked but background of checked items is not changing. I know a way how to do it, have your CustomAdapter and in getView() set the background of the views that are checked. But I believe there is much simpler way to handle it. Could you tell me if there is anyway to achieve it?
Attaching the Screenshot
Ok, i got the answer by going through the source code of other app which does that. Set a statelist drawable as background for list item. In selector state_activated = true give the background drawable.