I have a ListView and a Toolbar above of it in my Activity. I want to replace the Toolbar default icons (search and settings) with delete and edit icons when clicking an item of the ListView to perform some action.
So what I've understood from your question, you're looking for ActionMode which might serve your purpose.
So here's an implementation guideline.
Declare an ActionMode in your Activity and let your Activity to implement ActionMode.Callback.
public class YourActivity extends AppCompatActivity implements ActionMode.Callback {
// Declare ActionMode here
private ActionMode actionMode;
// Now implement the callback functions for ActionMode
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onCreateActionMode(final ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
// Inflate your menu here
inflater.inflate(R.menu.list_item_click_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_delete:
// Do something
actionMode.finish();
return true;
case R.id.action_edit:
// Do something
actionMode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
try {
this.actionMode = null;
// Do something. Reset the views maybe?
} catch (Exception e) {
e.printStackTrace();
}
}
}
Now to initiate your ActionMode you need to have this in your onClick function of the list item.
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (actionMode != null) {
return true;
}
// Show ActionMode
actionMode = startSupportActionMode(this);
actionMode.invalidate();
}
});
You may need to reset the ActionMode sometimes.
#Override
public void onResume() {
super.onResume();
if (actionMode != null) {
actionMode.finish();
actionMode = null;
}
}
Related
I've the base activity where all toolbar initializations and options menu are done, the activities extending the base can not fire onitemclick
In the base i have
public class BaseActivity extends AppCompatActivity {
private MenuItem refresh;
public Toolbar getToolbar() {
return toolbar;
}
public MenuItem getRefresh() {
return refresh;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
refresh = menu.findItem(R.id.action_refresh);
refresh.setActionView(R.layout.menu_item_view);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return false;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (mState) {
case Const.STATE_IDLE:
refresh.setVisible(true);
break;
case STATE_WORKING:
refresh.setVisible(false);
break;
default:
refresh.setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
}
In one of the activites I handle it like
public class CommentsActivity extends BaseToolbarActivity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.action_refresh){
setState(Const.STATE_WORKING);
showMsg(contentRoot,"oops");
return true;
}
return super.onOptionsItemSelected(item);
}
}
but the click items won't fire
After a while I came to realize that since I was setting a custom layout for my items (useful for animations) an option menu with a custom view that is either set in the xml or dynamically using
item.setActionView(R.layout.menu_lay);
Just like my problem above the menu item can never be invoked by the normal onOptionsItemSelected listener so the way to make it work is to implement onClickListener on the item's custom View so in my case the way to make it invoke is by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getRefresh.getActionView().setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//refresh some data
}
});
return true;
}
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 :)
I have an ActionBarActivity that has two tabs. I have implemented a ActionMode.Callback in tab1(fragment1). Now i want to implement it in tab2(fragment2) as well. The problem is that the tab1 Contextual action bar is active even after i swipe to tab2. This means a CAB is meant to be implemented for an activity, not for specific fragment.
So what is the approach to take to have different CABs in two fragments hosted in the same activity?
In the onCreateActionMode callback save a reference of the ActionMode in your fragment, and then in your fragment's onPause you can finish your action. Example:
protected class MultiChoiceModeListener implements AbsListView.MultiChoiceModeListener {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
ConversationFragment.this.actionMode = mode;
MenuInflater menuInflater = getActivity().getMenuInflater();
menuInflater.inflate(R.menu.menu_delete_conversation, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
ConversationFragment.this.actionMode = null;
}
}
#Override
public void onPause() {
super.onPause();
if (actionMode != null) {
actionMode.finish();
}
}
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
I've googled a lot, and find some tutorials and answers here in stackoverflow, but I am facing some difficults to resolve this issue.
I have a Fragment with a WebView, and I want to show my custom Contextual Action Bar when the user selects some text of my web view. I have two main issues here:
1 Currently, my custom CAB is showed when the user performs long click in any part of the web view.
2 The text is not selected when the user performs long click in some text of my web view.
Some of my current code:
Custom interface:
public class SelectActionModeCallback implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
Custom WebView
public class CustomWebView extends WebView {
private SelectActionModeCallback actionModeCallback;
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public ActionMode startActionMode(Callback callback) {
actionModeCallback = new SelectActionModeCallback();
return super.startActionMode(actionModeCallback);
}
In my fragment, I have this:
#Override
public void onResume() {
super.onResume();
myWebView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (mActionMode != null) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_SHORT).show();
return false;
}
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
private SelectActionModeCallback mActionModeCallback = new SelectActionModeCallback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.custom, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sustom:
customMethod();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
THERE IS A BETTER WAY! See the following answer: https://stackoverflow.com/a/22391169/2608235
In short, you can't use an OnLongClickListener and keep the selection within a WebView. Android does some weird stuff behind the scenes for selecting text within WebViews. If you override OnLongClickListener in order to call startActionMode, you will lose the selection, as you have found out.
What you should do instead is override startActionMode in your fragment, rather than its parent View (in your case, CustomWebView).
I don't have the mod permission to mark this question as a duplicate, but it is.
See my question for more info: Use a custom contextual action bar for WebView text selection