I have an onLongClickListener set up for a layout. On long click I need a context menu with singe "Delete" option. What is the most simple way to manage that? Thanks
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ....
mView = someView;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Menu Title");
MenuItem remove = menu.add("Delete");
remove.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//doStuff...
}
});
super.onCreateContextMenu(menu, v, menuInfo);
}
and in onLongPress or something you can call openContextMenu
Related
I'm new to the Android development. I was trying to add a context menu to my app. I understood that by default it requires long click on the button to open the context menu. But I need to make it to appear on the single click. I tried all the other solutions here in stackoverflow but none of them are really helping me.
I have posted my code below. kindly let me know what are the modifications to be done to make it working.
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
just :
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
confirmButton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
confirmButton .performLongClick();
}
});
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
You can simply write:
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.showContextMenu();
}
});
I have set setOnCreateContextMenuListener to edittext. onCreateContextMenu method is called user long press on edittext. and it opens context menu with 'done' and 'copy' options.
But my question is how can I handle when user select done option or copy options?
Can I get any event when user click on done button or copy button. so I can get selected text via clip manager?
edit.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
Log.i("TAG", "onCreateContextMenu");//it is printing while context menu is created.
}
});
Thanks.
onContextItemSelected is the event you should use. but it has no View argument to access selected item. Here is a trick to access to selected View.
public class MainActivity extends Activity
{
protected View selectedView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
this.selectedView = v;
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getId())
{
case R.id.Context_Edit:
// access to view with this.selectedView
break;
case R.id.Context_Delete:
// access to view with this.selectedView
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
You can get your both button event like following example
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.lv1)
{
menu.setHeaderTitle("Select Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 0, "Delete");
}
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getTitle() == "Edit")
{
// do something
}
if(item.getTitle() == "Delete")
{
// do something
}
return true;
}
Hope it will help you.
You must to override the onLongClick of the editText and setting one CAB. This is example:
private ActionMode.Callback action = new ActionMode.Callback() {
#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 onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == android.R.id.copy){
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Log.i("CAB",String.valueOf(clipboard.getPrimaryClip().getItemAt(0).getText()));
}
return true;
}
};
private EditText edtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtText = (EditText) findViewById(R.id.edtText1);
registerForContextMenu(edtText);
edtText.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
((EditText)v).setTextIsSelectable(true);
((EditText)v).setCustomSelectionActionModeCallback(action);
return false;
}
});
}
So I've successfully connected a context menu pop up to a list view in a fragment. The items show up, but when I click on them, onContextMenuItemSelectedMenu() is ignored and instead onMenuItemClick() is called in the parent activity. How can I make it so when I click the context menu items onContextMenuItemSelectedMenu() is called in the fragment instead. Thanks.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("item1");
menu.add("item2");
menu.add("item3");
}
#Override
public boolean onContextItemSelected (android.view.MenuItem item){
Log.i("cTest", "clicked context menu");
return true;
}
I figured it out. It's turned out to be the same as buttons. Both in the fragment:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("item0").setOnMenuItemClickListener(this);
menu.add("item1").setOnMenuItemClickListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem item){
if(itemName.equals("item0))
{
}
else if (itemName.equals("item1"))
{
}
}
I'm trying to use a floating context menu and I wonder if it's possible to activate this menu, by pressing the image in the ImageView?
My first problem is how to handle registerForContextMenu and the ImageView? I searched and find most examples with GridView and ListViews.
I have made the menu in xml and I should I use this method in the activity with a switch:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
}
Just like the others, you get your View in onCreateContextMenu, based on that you inflate the menu for the proper item.
registerForContextMenu(imageView);
The method above expects any View class.
Each time you call registerForContextMenu() for a different View, onCreateContextMenu() will be called for you to handle the proper menu creation.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId == R.id.youtImageView) {
getMenuInflater().inflate(R.menu.image_menu, menu);
}
}
Based on the item id you decide for which View the menu was clicked. You must me sure the id's of menu items for different views are not the same.
When the item from a context menu is clicked, you will receive the onContextItemSelected() callback with MenuItem that was clicked
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.image_menu_item_do_something:
doSOmething();
return true;
default:
return super.onContextItemSelected(item);
}
}
Make sure that you have these methods in onCreate:
ImageView image = (ImageView) findViewById(R.id.image_view);
registerForContextMenu(image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(image);
}
});
And in context_menu_main.xml, that it looks similar to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_option"
android:title="#string/action_option_text" />
</menu>
Finally, you'll need to override these two methods as follows:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu_main, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_option:
Log.e("TAG", "Option");
return true;
default:
return super.onContextItemSelected(item);
}
}
The questions in the title, had a look around but couldnt find any answers,
thanks
Try setting a boolean in
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
and then unsetting it in
#Override
public void onContextMenuClosed(Menu menu) {
super.onContextMenuClosed(menu);
}