I Have a tablayout, within which in each tab, i have listviews that use custom adapters. I have setup the listview to Multi MODAL and defined a cabmenu. I have used the same cabmenu in another activity having similar tabs and listviews. I dont understand what i am missing , the cab menu is not appearing on long press on the listview items. The only difference between the other activity and this one is, the other has an action bar. Below is the code where the cab menu isnt working.
public void showLists(){
adapter=new CustomItemAdapter(getActivity(),listtype,fruititem,listid);
lv=(ListView) getView().findViewById(R.id.fruits);
lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
selection.add(fruititem.get(position));
count++;
} else {
selection.remove(fruititem.get(position));
count--;
}
TextView tv = (TextView) getActivity().getLayoutInflater().inflate(R.layout.contextual_title, null);
tv.setText(count + " selected");
mode.setCustomView(tv);
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater menuinflater = new MenuInflater(getContext());
menuinflater.inflate(R.menu.cabmenu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.deleteic) {
for (ItemLists s : selection) {
dbHandler.deleteItem(s);
}
adapter.notifyDataSetChanged();
mode.finish();
showLists();
}
mode.finish();
return true;
}#Override
public void onDestroyActionMode(ActionMode mode) {
count = 0;
selection.clear();
}
});
}
Related
Is it possible to focus the List View Items through any button click?
Like i want that when user click on Floating Action Button then the listview gets focused. I dont want to show checkbox type layout on button clicked. I just want to show the same like in the screenshot on Button click.
What i did is I put the listview onItemLongClick code in the button click blocks but it doesnot work.
fabButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
listViewMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listViewMessages.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
tv.setText(listViewMessages.getCheckedItemCount()+ " Selected");
}
#Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
}
});
mode.finish();
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.contextual, menu);
fabButton.setVisibility(View.INVISIBLE);
fabButtonn.setVisibility(View.VISIBLE);
fabButtonn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for ( int i=0; i< messageListAdapter.getCount(); i++ ) {
listViewMessages.setItemChecked(i, true);
}
}
});
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
fabButton.setVisibility(View.VISIBLE);
fabButtonn.setVisibility(View.GONE);
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
With this code if user clcik on button then he/she has too long press items again to focus the list items which is not what i want. I want to focus items right when button click. Any explanation or link provided will be helpful
I am adding a menu item for texts selection. normally it shows cut,copy,share, etc.. I added a one more item to this menu and named the item as "Mark".
For this I added the following code in my Activity.Java
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.add("Mark");
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}
I am able to get my menu item while long press on texts.. Below is the screen, which reflects my menu item.
For this menu item, I want to do something while select it. So, I used the following code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if( item.getTitle().equals("Mark")){
System.out.println("MenuItem Mark clicked----");
Log.d("MenuItem clicked----", "Mark");
}
return super.onOptionsItemSelected(item);
}
Here, I am not able to get the message "MenuItem Mark clicked----" or "MenuItem clicked---- Mark" in my Logcat.
How may I do this?
update
Followed Elitz's answer, but still no luck. my Changes below
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.add(0,1000,0,"Mark");
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 1000) {
System.out.println("MenuItem Mark clicked----");
Log.d("MenuItem clicked----", "Mark");
}
return super.onOptionsItemSelected(item);
}
Added updated answer and still no messages came in Logcat
private ActionMode.Callback startActionMode = (new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu yourMenu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu yourMenu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem yourMenu) {
Log.d("MenuItem clicked----303", "Mark");
if (yourMenu.getItemId() == R.attr.actionModeSelectAllDrawable) {
System.out.println("MenuItem Mark clicked----305");
Log.d("MenuItem clicked----", "Mark");
}
return true;
}
});
update2
Followed Ankesh's answer also,
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.add(R.id.privateText);
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.privateText) {
System.out.println("MenuItem Mark clicked----");
Log.d("MenuItem clicked----", "Mark");
}
return super.onOptionsItemSelected(item);
}
Both attempt there is no logs available...
Update3
For test the other menu, I found id for selectAll menu item in R,java, and tried the following code,
#Override
public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.attr.actionModeSelectAllDrawable) {
System.out.println("MenuItem All clicked----");
Log.d("MenuItem clicked----", "All");
}
return super.onOptionsItemSelected(item);
}
this is also not showing the message in Logcat. Is this function is right one for menu items selection. or what else I am missing in this?
use this instead of your menu.add("Mark");
add(int groupId, int itemId, int order, CharSequence title);
now you have your Id now you can check for that.
groupId = 0; and order = 0;, or any number that fits your choice, but since in your example you have only 1 group, just put 0.
hmm.. i think you got us all fooled :) yes it will not work, because you are using ActionMode right and with ActionMode you need to specify a callback for it, so you should put your code here
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem arg1) {
return false;
}
in its callback. like something like this, when you call startActoinMode
startActionMode(new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu yourMenu) {
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu yourMenu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem yourMenu) {
//put your item click here
return false;
}
});
Use
if (item.getItemId() == R.id.xyz) {
}
Can you just add your menu item inside your xml menu(I mean inside R.menu.main)? And after that just detect the item click inside this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_id:
//TODO click detected!!
break
default:
return super.onOptionsItemSelected(item);
}
}
Its that or I don't understand your question :)
Am using CHOICE_MODE_MULTIPLE_MODAL for a ListView and it is working fine. problem is my ListView row item contains 2 buttons. And i want this all row buttons set to be disable when i have some rows checked.How to achieve this?
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
final int checkedCount = studentListView.getCheckedItemCount();
mode.setTitle(checkedCount + " selected");
adapter.toggleSelection(position);
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
SparseBooleanArray selected;
switch (item.getItemId())
{
case R.id.menu_item1:
mode.finish();
return true;
case R.id.menu_item2:
mode.finish();
return true;
case R.id.menu_item3:
mode.finish();
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.multiselectmenu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
adapter.removeSelection();
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});
// Add one boolean in your model class and check same condition in adapter
raw_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (<booleanvar==true>) {
//
}
}
});
Check when have some rows checked.
Call buton.setEnabled(false) for button you want to disable.
I want to enable multiple view selection on longClick(). Should I declare an action mode object and call startActionMode()? Also, how would I change menu list for single item click? I used the documentation as reference, as shown.
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 invalidate() request
return false;
}
});
To change menu list for single item click following is the code.
int count=0;
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
if (checked) {
count++;
} else {
count--;
}
mode.invalidate(); // Add this to Invalidate CAB so that it calls onPrepareActionMode
}
Now modify the onPrepareActionMode as follows
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selCount == 1){
//show the menu here that you want if only 1 item is selected
} else {
//show the menu here that you want if more than 1 item is selected
}
}
I have a listActivity that shows CAB on long click. If more than 1 item is selected I would like to hide one of my menu items.
I keep track of the # of items selected in onItemCheckedStateChanged(). However I don't have access to the menu to remove the item from this function. See comments in code below to get an idea of what I was trying. I feel like I am missing some simple core understanding... code below is called from my onCreate() function.
private void setupActionBarContext() {
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
private int selCount = 0;
ArrayList<Long> idList = new ArrayList<Long>();
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
selCount++;
idList.add(id);
} else {
selCount--;
idList.remove(id);
}
mode.setTitle(selCount + " selected");
// I WOULD LIKE TO HIDE ITEM ON MENU IF 'selCount' IS > 1
// For example something like this...
// if (selCount > 1) {
// MenuItem item = menu.findItem(R.id.edit_item);
// item.setVisible(false);
// } else {
// MenuItem item = menu.findItem(R.id.edit_item);
// item.setVisible(false);
// }
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_item:
for(Long i: idList){
mDbHelper.deleteItem(i);
}
mode.finish();
return true;
case R.id.edit_item:
Toast.makeText(getBaseContext(), "Edit Item", Toast.LENGTH_SHORT).show();
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, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
selCount = 0;
idList.clear();
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});
And my menu item...
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/edit_item"
android:title="#string/edit_item"
android:showAsAction="ifRoom"
android:orderInCategory="1"/>
<item android:id="#+id/delete_item"
android:title="#string/delete_item"
android:icon="#drawable/ic_action_delete"
android:showAsAction="ifRoom"
android:orderInCategory="2"/>
</menu>
As suggested in adneal's comment.
Add invalidate() to onItemCheckedStateChanged()
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
selCount++;
idList.add(id);
} else {
selCount--;
idList.remove(id);
}
mode.setTitle(selCount + " selected");
mode.invalidate(); // Add this to Invalidate CAB
}
This invalidates the CAB and causes the onPrepareActionMode() function to be called.
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selCount == 1){
MenuItem item = menu.findItem(R.id.edit_item);
item.setVisible(true);
return true;
} else {
MenuItem item = menu.findItem(R.id.edit_item);
item.setVisible(false);
return true;
}
}