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);
}
}
Related
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);
}
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;
}
});
}
I use this code to override webview Context Action Bar but during longclick the CAB appears but no text selection how can i only fire CAB when text selection mode?
public class MainActivity extends Activity {
ActionMode.Callback mCallback;
ActionMode mMode;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("http://www.google.com");
webview.setOnLongClickListener(new OnLongClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (mMode != null)
return false;
else
mMode = startActionMode(mCallback);
return true;
}
});
mCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mMode = null;
}
#SuppressLint("NewApi")
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("1 selected");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi")
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getBaseContext(), "Selected Action1 ",
Toast.LENGTH_LONG).show();
mode.finish();
break;
}
return false;
}
};
}
}
during long press my custom CAB appear without text selection
how to make it appear only in the case of text selection?
many thanks
don't do this,it is not necessary to overwrite longClick, you should make your custom webview and do some thing that below link display:
Look At This Answer
When i do a long click on an item in the list view(which triggers the contextual action bar to show up), the list view automatically scrolls to the end. This behaviour is very irritating if the user had selected an item after scrolling up.
Using breakpoints i verified that this happens after onItemCheckedStateChanged() in the MultiChoiceModeListener implementation has completed. But i am not sure what code gets executed after this that causes the behaviour.
Removing the transcriptMode attribute from the list view layout resolves the issue. But i don't want to remove it as it is required to scroll automatically when the data has changed in the cursor. Any ideas what is causing the problem?
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get the chat messages list view from the layout
lv_chatMessages = (ListView) getActivity().findViewById(
R.id.listview_chat);
// Instantiate the adapter for the chat messages
adpt_chat = new ChatMessagesAdapter(getActivity());
// connect the adapter to the list view
lv_chatMessages.setAdapter(adpt_chat);
//Implement multi choice mode listener for the list view
//only if Android API 11 or higher
if (Utils.hasHoneycomb()) {
//Enable selection of multiple chat messages
lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
//Handle Action mode events
lv_chatMessages
.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
switch(item.getItemId()){
case R.id.delete_menu :
long[] selected_IDs = lv_chatMessages.getCheckedItemIds();
int deletedRows = deleteMessages(selected_IDs);
Toast.makeText(getActivity(), deletedRows + " message(s) deleted",
Toast.LENGTH_SHORT).show();
return true; // true indicates the menu selection is handled. no further
// system handling required
default :
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode,
Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.chatsession_contextmenu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onPrepareActionMode(ActionMode arg0,
Menu arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
mode.setTitle(lv_chatMessages.getCheckedItemCount() + " selected");
}
});
}
...
..
}
The list view layout :
<ListView
android:id="#+id/listview_chat"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
android:layout_above="#id/layout_input"
android:divider="#00000000"
/>
Removing the transcriptMode attribute seems to be the only solution. I use the below code whenever i require an automatic scroll to the end :
new Handler().postDelayed(new Runnable(){
public void run(){
lv_chatMessages.setSelection(lv_chatMessages.getCount());
}
},100);
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();
}