I am trying to show and hide the menu and toolbar in the adapter class. The elements show fine but they don't hide as expected. This is my implementation
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater menuInflater = mode.getMenuInflater();
toolbar.setVisibility(View.GONE);
menuInflater.inflate(R.menu.menu, menu);
return true;
}
public void onDestroyActionMode(ActionMode mode) {
mode.getMenu().clear();
toolbar.setVisibility(View.VISIBLE);
mode.finish();
}
I want to hide that menu which inflates in onCreateActionMode() method.The "menu" does not disappear as it says that mode.getMenu().clear() is implemented on null object. How do I hide menu in onDestroyActionMode ? Any suggestion would be helpful Thanks.
Put this code in your button click listener. it will definitely work.if (getSupportActionBar() != null) {getSupportActionBar().hide();} Just like this.
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
return false;
}
});
Related
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;
}
}
I have a contextual action bar, where I have a setting called "DELETE". When I press that button I want the CAB menu to disappear.
class MyActionModeCallBack implements android.view.ActionMode.Callback {
#Override
public boolean onCreateActionMode(android.view.ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.event_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(android.view.ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.view.ActionMode actionMode, MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.delete) {
*******
_root.removeView(view);
// This is where I want to remove the CAB menu
*******
}
return false;
}
#Override
public void onDestroyActionMode(android.view.ActionMode actionMode) {
((RelativeLayout)view).removeAllViews();
view.setBackgroundColor(0xFF00FF00);
view.setTag(R.string.viewSelected, "0");
}
}
I was thinking about calling the onDestroyActionMode() right after the _root.removeView(view);, but I don't know what arguments to pass in.
Any suggestions are appreciated, thanks!
If you are trying to hide the item being selected, you can call
menuItem.setVisible(false);
Alternatively, if you want to remove all items from the menu, you can call
optionsMenu.clear();
in the onActionItemClicked() method.
You can save your menu to a field in your onCreateOptionsMenu method as follows:
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
optionsMenu = menu
}
Call actionMode.finish(). This will invoke the onDestroyActionMode() callback.
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'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
How to set navigation bar in android and how to add option button on navigation bar ?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menuDialog == null) {
menuDialog = new Dialog(this, R.style.Dialog_Fullscreen);
menuDialog.setContentView(menuView);
menuDialog.show();
} else {
menuDialog.show();
}
return false;
}