How to properly trasfer contextual action bar functionality to a toolbar - android

I have an activity that shows some data on a listview. when one long press an item, i have implemented a toolbar that becomes visible and allows the user to perform some actions. i don't want this actions to be at contextual action bar since my actionbar displays some information that show not be overtaken by contextual action bar
When one long press a list item, my toolbar become visible and you can do some actions like deleting.
desired output
contextual action bar should not show up when you long press a list item
when you press your phone back button when you have not performed any action but just selected list items. list selection should dissapear BUT NOT exiting the activity
Problems
after long pressing a list item, contextual action bar appears
after performing the action(e.g delete), contextual action bar still remains visible.
When i select list items and just click back button,my activity is exited instead of just disselecting list selections
here's how am handling long clicks on my listview
private class DataSelector implements AbsListView.MultiChoiceModeListener{
android.view.ActionMode mode;
#Override
public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
adapter.toggleSelection(position);
}
#Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
this.mode =mode;
SelectionListener selectionListener = new SelectionListener();
toolbar.setVisibility(View.VISIBLE);
Button delete = (Button) toolbar.findViewById(R.id.Delete);
delete.setOnClickListener(selectionListener);
return true;
}
private class SelectionListener implements View.OnClickListener{
public void onClick(View v){
switch(v.getId()){
case R.id.Delete:
SparseBooleanArray selected = adapter.getSelecteItemsIds();
ArrayList<Long> dataIds = new ArrayList<>();
for(int i =(selected.size()-1); i>=0; i--){
if(selected.valueAt(i)){
int key = selected.keyAt(i);
d(" key is: " + key);
CountryData data = adapter.getItem(key);
long id = data.getId();
if(id != 0){
dataIds.add(id);
}
adapter.remove(data);
}
}
onDestroyActionMode(mode);
break;
}
}
}
#Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(android.view.ActionMode mode) {
adapter.removeSelection();
toolbar.setVisibility(View.INVISIBLE);
}
}
Question
how can i achieve the above desired output?

Related

Android CAB destroying after listview notifydatasetchanged

I am using setMultiChoiceModeListener on Listview to invoke Action Mode for selecting multiple items to be deleted. On ListView scroll I load more data from server and notify the adapter. After adapter has been notified, the Action Mode (if invoked) is destroyed and recreated, which makes the title and selected arraylist empty (You can see the picture below). While the listview item selection remains. I want the CAB to be persistent like we see it in Gmail app, where it does not destroy on loading more data.
Below is the code for Action Mode
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Item mItem = adapter.items.get(position);
if (mItem.isSection()) {
} else {
mode.setTitle(listView.getCheckedItemCount() + " Selected");
EntryItem mEntryItem = (EntryItem) mItem;
orderid = mEntryItem.orderId;
if (checked) {
selectedIdList.add(orderid);
} else {
selectedIdList.remove(orderid);
}
// Toggle the state of item after every click on it
adapter.toggleSelection(position);
}
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.delete){
if (selectedIdList.size() > 0) {
deleteItems(selectedIdList);
}
mode.finish();
return true;
}
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_main, menu);
actionMode = mode;
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
if (selectedIdList.size() > 0) {
selectedIdList.clear();
adapter.mSelectedItemsIds.clear();
}
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});
Any help will be highly appreciated, thanks in advance.
You need to save selected items id in order to retain state and show as selected after you refresh/notify adapter changes like shown in below examples:
1) https://androidperspective.wordpress.com/2013/04/17/contextual-action-bar-with-listview/
(It shows selection and saving selected item ids in array to save state).
2) http://theopentutorials.com/examples/android/listview/android-contextual-action-bar-for-listview-item-deletion-using-actionbarsherlock/
(It shows complete example you want for deletion just using sherlock action bar it would be little change only).
Glad to know if it helps you.

Extracting words from TextView after click/press on them

is there any easy way to select whole words from TextView by touching them? This functionality is in dictionary app ColorDict.
Search for word "word"
Select word "theorem" and it now appears in the search box so I can search it faster by clicking on search.
I want to be able to select those words. That ScrollView which is on top is misleading. It appears after selecting "Select a word" in dialog which appears after long press on something in TextView (you can see word "theorem" on the bottom - long press on "theorem").
Thank you for suggestions.
Try setting this attribute to your TextView:
android:textIsSelectable="true"
EDIT:
private static final int MENU_ITEM_ID = 0x42;
private TextView targetTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
targetTextView = (TextView) findViewById(R.id.target_textview);
targetTextView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// you can remove the default menu items if you wish
menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.cut);
menu.removeItem(android.R.id.copy);
return true;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
/*
* called when the action mode is created. Here you can
* generate action buttons for this action mode.
* */
menu.add(0, MENU_ITEM_ID, 0, "Menu Item").setIcon(android.R.drawable.ic_dialog_alert);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// called when the action mode is about to be destroyed
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ID:
int start = targetTextView.getSelectionStart();
int end = targetTextView.getSelectionEnd();
CharSequence selectedText = targetTextView.getText().subSequence(start, end);
Toast.makeText(MainActivity.this, selectedText, Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
break;
}
return false;
}
});
}

Activate CAB menu when OnClickEvent happens in Android

Trying to activate CAB menu when clicking on MenuItem from ActionBar. Here is how I set the GridView for listening to Multi Choice. The multiModeChoiceListener is working fine when I long press on Any item in the GridView. It is working fine. Now I have a requirement to activate the CAB menu when do press on a menu item in Action Bar. Once it is pressed, the CAB menu should read that 0 items are selected. After that it should allow me to select items from GridView on single clicks. How can I achieve this feature?
GridView set listener:
gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gv.setMultiChoiceModeListener(new MultiChoiceModeListener());
MultiChoiceModeListener.java
public class MultiChoiceModeListener implements
GridView.MultiChoiceModeListener {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.featured_multiselect, menu);
MenuItem mi = menu.findItem(R.id.close);
mi.setIcon(R.drawable.cancel);
mode.setTitle("Select Items");
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle(),
Toast.LENGTH_SHORT).show();
if (item.getTitle().toString().equalsIgnoreCase("Close")) {
mode.finish();
}
return true;
}
public void onDestroyActionMode(ActionMode mode) {
new ChangeNotifier().changeOnFavoriteStore = true;
new AddFavorites().execute("add", device_id, dataArray);
if (notify == true) {
Toast.makeText(getApplicationContext(),
"Selected items are added to Favorites",
Toast.LENGTH_SHORT).show();
notify = false;
}
}
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
int selectCount = gridView.getCheckedItemCount();
if (selectCount > 0) {
notify = true;
dataArray.add(position);
switch (selectCount) {
case 1:
mode.setSubtitle("One item added to favorites");
break;
default:
mode.setSubtitle("" + selectCount
+ " items added to favorites");
break;
}
}
}
OnMenuItemClick method:
public boolean onPrepareOptionsMenu(final Menu menu) {
final MenuItem editItem = menu.findItem(R.id.editit);
editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//the CAB menu should be activated here. So that it reads that 0 items are selected in ActionBar
return false;
}
});
From your question I understand that you're trying to start the GridView associated CAB from clicking one of the menu items. I don't know if you can do this(but I may be mistaken) as the MultiChoiceModeListener expects an item to be checked to start. Depending on your layout and the overall appearance of the GridView, I think you could have a dummy item(as an extra item in the adapter) at the end of the GridView(with no content showing) and use setItemChecked(dummyItemPosition, true) to start the GridView CAB. Of course you'll need to have additional logic to take care of that extra item in your MultiChoiceModeListener:
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
if (position == theDummyPosition)
return; // so we start the CAB but there aren't any items checked
}
int selectCount = gridView.getCheckedItemCount();
if (selectCount > 0) {
notify = true;
dataArray.add(position);
// if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it
switch (selectCount) {
case 1:
mode.setSubtitle("One item added to favorites");
break;
default:
mode.setSubtitle("" + selectCount
+ " items added to favorites");
break;
}
}
}
The solution above is a hack, most likely it would be much easier to lose the MultiChoiceModeListener and simply start an ActionMode that you can manipulate for both situations.

Go back after Searching with setActionView in Android using ActionBarSherlock

With this code (taken from ABS examples) I'm able to add a "Search" icon in my ActionBar which will display an EditText when clicked (to search in a list):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, searchId, Menu.NONE, R.string.menu_search)
.setIcon(android.R.drawable.ic_menu_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
With this code I'm able to show a different layout after tapping the "Search" icon:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case searchId:
search = (EditText) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
setContentView(R.layout.search);
this.allSongs = data.getSongs();
ListView list = (ListView) findViewById(R.id.songList);
list.setAdapter(new SongAdapter(this, this.allSongs));
}
return true;
}
With this code I'm able to handle my search:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<Song> results = new ArrayList<Song>();
Locale locale = Locale.getDefault();
for (Song song : allSongs) {
if (song.getTitle().toLowerCase(locale).contains(s.toString().toLowerCase(locale)))
results.add(song);
}
ListView list = (ListView) findViewById(R.id.songList);
list.setAdapter(new SongAdapter(ctx, results));
}
};
When the user taps the "Search" icon, the EditText appears in ActionBar and the App Icon gains the "back" left arrow.
If the user taps "Back" button or taps the ActionBar icon (with left arrow), the EditText disappears.
I'd like to intercept this event (no, onOptionsItemSelected() doesn't fire this time) and show the previous layout, in addition to hiding the EditText.
Note: I need API8 (Android 2.2) compatibility!
Thank you!
When using an ActionView, tapping "up" or "back" doesn't result in an Ancestral Navigation (which can be handled by onOptionsItemSelected) but results in a Collapsible Action event.
Found here on Android Developers.
Handling collapsible action views
If you need to update your activity based on the visibility of your
action view, you can receive callbacks when it's expanded and
collapsed by defining an OnActionExpandListener and registering it
with setOnActionExpandListener(). For example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
MenuItem menuItem = menu.findItem(R.id.actionItem);
...
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}
So, to answer to my question, I was searching for onMenuItemActionCollapse event.
PS: for better consistency I should move my case searchId: from onOptionsItemSelected to onMenuItemActionExpand
For the Home button, can I assume that you called
mActionBar.setHomeButtonEnabled(true);
when setting up the Sherlock Action Bar in onCreate()? I believe that's required in order to get onOptionsItemSelected() callbacks with an item ID of android.R.id.home when the home button is clicked.
For the back button, I think you just need to override onKeyDown in your activity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
// Do something
return true;
}
return super.onKeyDown(keyCode, event);
}

Contextual Menu in a Fragment inside a Fragment (ActionMode)

I got a Fragment B in a Fragment A in a Activity. Works as expected.
When clicking an item in Fragment B, I want to display a contextual menu bar.
I am working with ActionbarSherlock.
What i've done this inside my Fragment B:
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.entry_list_context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// make sure no item is selected when bar is shown
adapter.clearSelection();
adapter.notifyDataSetChanged();
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.d("EntryList", "Item '" + item.getTitle()
+ "' clicked [onActionItemClicked()]");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
adapter.clearSelection();
adapter.notifyDataSetChanged();
contextualMode = null;
}
};
private ActionMode contextualMode;
#Override
public void onItemClick(AdapterView<?> parentView, View itemView,
int index, long id) {
DocumentEntity entry = (DocumentEntity) itemView.getTag();
// something went wrong
if (entry == null) {
Log.e("EntryList", "Tag-Less item clicked [onItemClick()]");
return;
}
if (contextualMode != null) {
Log.d("EntryList",
"contextualMode is not yet initialized [onItemClick()]");
contextualMode = getSherlockActivity().startActionMode(
mActionModeCallback);
} else {
Log.d("EntryList",
"contextualMode already initialized [onItemClick()]");
}
entry.setSelected(!entry.isSelected());
Log.d("EntryList", "entry.selected set to " + entry.isSelected()
+ " [onItemClick()]");
}
The selection works pretty good, but no contextual Actionbar is shown.
The debug result is:
contextualMode already initialized [onItemClick()] entry.selected set
to 'true' [onItemClick()]
There is no other position where contextualMode is set...
I got a Fragment B in a Fragment A...
Android does not support embedding a fragment within another fragment. Sorry. This leads me to believe that your problem goes beyond the fact that your contextual ActionBar is not being shown. I suggest you clarify your original post.

Categories

Resources