Android: Menù popup with long click? - android

I need to implement a menù that appears with a long click around a button, so the user can choose with option he wants to take simply sliding toward a certain direction. Is there any way to do that? I have just a setOnLongClickListener with a onLongClick method for now.
btn01.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick (View view) {
Toast.makeText(getApplicationContext(),"Button 01 long clicked", Toast.LENGTH_SHORT).show();
return true;
}
});

In your Activity :
btn01.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick (View view) {
registerForContextMenu(btn01);
openContextMenu(btn01);
return true;
}
});
#Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
//Context menu
menu.setHeaderTitle("My Context Menu");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Add");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Edit");
menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Delete");
}
#Override
public boolean onContextItemSelected (MenuItem item){
switch (item.getItemId()) {
case CONTEXT_MENU_VIEW: {
}
break;
case CONTEXT_MENU_EDIT: {
// Edit Action
}
}
}

Related

how to know what view is clicked to open context menu?

i have two button where i register a context menu
but1=(ImageButton)findViewById(R.id.imageViewX);
but2=(ImageButton)findViewById(R.id.imageViewY);
registerForContextMenu(but);
registerForContextMenu(but2);
I have a problem in onContextItemSelected(MenuItem item) how to know if user click but1 or but2 ?
with id=item.getItemId(); i have the id of item selected, but i want to know what button is clicked in onContextItemSelected method.
When you create contextitem create unique ids for each of the two Imagebuttons
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
{
if(view.getId() == R.id.imageViewX)
menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "imageViewX");
else
menu.add(Menu.NONE, Menu.FIRST+10, Menu.NONE, "imageViewY");
super.onCreateContextMenu(menu, view, menuInfo);
}
And in
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == (Menu.FIRST+1)) {
//do something
}else if(item.getItemId() == (Menu.FIRST+10)){
//do something else
}
}

onContextItemSelected Error

I having some difficulty implementing a context menu into my android application. My first problem was I was trying to implement OnCreateContextMenu inside of OnCreate but I kept getting an error saying:
void is an invalid type for the variable onCreateContextMenu
I fixed this problem by putting onCreateContextMenu outside of OnCreate. Now my problem lies with OnContextItemSelected. My error occurs on the line: public boolean onContextItemSelected(MenuItem menu). The errors are:
implements android.view.View.OnLongClickListener.onLongClick
Syntax error, insert "}" to complete MethodBody
Here is the code:
BaconStripsButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
boolean onContextItemSelected(MenuItem item)
{
if (item.itemId() = 0)
{
Toast ringtone = Toast.makeText(startingPoint.this, "Ringtone added Successfully!", Toast.LENGTH_SHORT);
return true;
}
return false;
}
}
});
Any help would be appreciated. Thanks, Justin
No need to use onContextItemSelected Inside onlongClick of button.Just Override OnContextItemSelected(); and register ContextMenu to btn.No need to setOnlongClickListener.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
Then override
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then finally registerContextMenu(button);
You may use the ListView and implement the onCreateContextMenu in the OnCreate of the Activity.
like this:
myList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
// add some sublist
menu.setHeaderTitle(R.string.collect_title);
menu.add(0, 1, 0, R.string.delete_string);
menu.add(0, 2, 0, R.string.move_to_project_string);
menu.add(0, 3, 0, R.string.move_to_action_string);
}
});

display context menu for position "2" click in list view only android

I want to display context menu for list position clicked means only for 2 nd position clicked in list view so how to do it.
I had implemented the code for displaying context menu for each position clicked. So how to make it specific for any position in ListView.
my code for displaying context menu for each position in list view ..pls do required modification on my code thanks...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenulist);
// registerForContextMenu(getListView());
CustomAdapter adapter = new CustomAdapter(
this, R.layout.listitem,R.id.title, data
);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
#Override
public void onCreateContextMenu(ContextMenu menu,
View v,ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Gallery");
menu.add(0, v.getId(), 0, "Camera");
menu.add(0, v.getId(), 0, "Cancel");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Gallery"){
function1(item.getItemId());
} else if(item.getTitle()=="Camera"){
function2(item.getItemId());
} else return false;
return true;
}
public void function1(int id){
Toast.makeText(this, "Gallery function called",
Toast.LENGTH_SHORT)
.show();
}
public void function2(int id){
Toast.makeText(this, "Camera function called",
Toast.LENGTH_SHORT)
.show();
}
In your code I can see only onCreateOptionsMenu() method. In order to restrict the options menu as you wish, you have to add one more method to it.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}
And now when you click on the listview get the position of the item that is being clciked and store it globally. Now change the above method like this,
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if(list_item_position==2){
return true;
}
else
return false;
}
Now your options menu will pop up only if the list item in second position is clicked.

Invoke android context menu on menu item press

Can anyone kindly guide as to how can I invoke a context menu on the press of a menu item. I googled a lot for the same, but nothing turned up.
Look forward for your valuable help.
Regards,
Rony
You are probably looking for openContextMenu(view). Call it in your Menu's onclick()
To create a context menu, override onCreateContextMenu and onContextItemSelected. Refer google for examples.
You only need to implement this function. It will work.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.e(LOGTAG, "Tao menu");
if(v == expList)
{
super.onCreateContextMenu(menu, v, menuInfo);
//AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
//HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options");
menu.add(1, 1, 1, "Reprint");
menu.add(1, 2, 1, "Void");
menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
}
}

How to remove an array item from a context menu?

I have a ListView and would like to remove a row item when the user long clicks on selects Remove from the context menu.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Selection Options");
menu.add(0, v.getId(), 0, "Remove Symbol");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Remove Symbol"){
Toast.makeText(this, "Remove clicked!", Toast.LENGTH_SHORT).show();
}
else {
return false;
}
return true;
}
How can I get a reference to the row number that was clicked, so I can remove that index from my array?
In your onContextItemSelected callback, you can use this code to get the id of the item.
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
removeItemFromListById(info.id);
}
Source:
Creating Menus | Android Developers

Categories

Resources