how to add spinner to contextual action bar? - android

like my tittle, i want to ask...
how to add spinner on contextual action bar like in galery android?
now i just can set the tittle like "1 selected" "2 selected"
here`s my code
public void lvMainOnLongItemClick(){
lvMain.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lvMain.setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#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
adapter.clearSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.canvas_main, menu);
menuEdit = menu.findItem(R.id.menu_edit);
menuUpload = menu.findItem(R.id.menu_upload);
menuUpload.setVisible(false);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_edit:
Toast.makeText(rbkMain.this, "Edit", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_cancel:
Toast.makeText(rbkMain.this, "Cancel", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_copy:
Toast.makeText(rbkMain.this, "Copy", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_upload:
Toast.makeText(rbkMain.this, "Upload", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(rbkMain.this, "Yihaa", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
adapter.setNewSelection(position, checked);
} else {
nr--;
adapter.removeSelection(position);
}
if(nr > 1)
menuEdit.setVisible(false);
else
menuEdit.setVisible(true);
mode.setTitle(nr + " selected");
}
});
lvMain.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
lvMain.setItemChecked(position, adapter.isPositionChecked(position));
return false;
}
});
thx a lot for your help... happy coding

For regular ActionBar this worked for me Adding spinner to ActionBar (not Navigation but for the contextual action bar it was not working. This is how I made it work, see if it can help you.
For your listview listener something like this:
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(YOUR_MENU_XML, menu);
filterSpinner = (Spinner) MenuItemCompat.getActionView(menu.findItem(R.id.menu_overlay_spinner));
filterSpinner.setAdapter(send2Adapter);
return true;
}
The menu xml should have something like this:
<item
android:icon="#drawable/filter"
android:id="#+id/menu_overlay_spinner"
android:title="Spinner"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="ifRoom"
android:actionLayout="#layout/YOUR_ACTION_LAYOUT"/>
Finally the actionLayout is just a layout with a Spinner as the root:
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

In onCreate of main activity
// This has to be called before setContentView and you must use the
// class in android.support.v4.view and NOT android.view
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
To show/hide progress in action bar. Notice with actionbarsherlock you must use boolean.TRUE/FALSE, not just true/false.........
if (getSupportLoaderManager().hasRunningLoaders()) {
setProgressBarIndeterminateVisibility(Boolean.TRUE);
} else {
setProgressBarIndeterminateVisibility(Boolean.FALSE);
}

Related

showing contextual action bar at other position

I am using a gridview of photos. Long pressing a photo will start to count the number of selected photos. Coding as follows:
Coding:
gd_view.setMultiChoiceModeListener(new AbsListView.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 onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
int selectCount = gd_view.getCheckedItemCount();
switch (selectCount)
{
case 1:
mode.setSubtitle("1 item selected");
break;
default:
mode.setSubtitle("" + selectCount + " items selected");
break;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
mode.setTitle("Select Items");
mode.setSubtitle("1 item selected");
return true;
}
});
Question:
Instead of showing contextual action bar at the top, can I inflate a custom menu at any desired location, e.g. bottom of my app so as to select / dis-select items ? I would like to share the selected photos afterwards.
You can do that by using support Toolbar like this:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
You can place this toolbar anywhere in your activity's XML file.
You need to set this toolbar as action bar in your activity like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}

Focus ListView Items on Button Click

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

CAB menu not working

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();
}
});
}

disable button click of all listview item when choice_mode is on

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.

Hide Items in Context Action Bar (CAB) dynamically when multiple items are 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;
}
}

Categories

Resources