I want show a menu when longClick - android

i have 1 app in the play store and i want do a new menu for him.
When a item of a list view is press i want show a menu like a whatsapp.
i have this.
What I have to do to display a menu like the whatsapp?
lista is a listview
lista.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
registerForContextMenu(lista);
return true;
}
});
now i use contextmenu, but when i click long in item the contextmenu no works
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Menú");
menu.add(0, v.getId(), 0, "Abrir");
menu.add(0, v.getId(), 0, "Cambiar nombre");
menu.add(0, v.getId(), 0, "Borrar");
menu.add(0, v.getId(), 0, "Poner alarma");
}

I believe what you're looking for is ContextMenu.
Reference
Guide

1º declare the items for a context menu in
/res/menu/NAMEOFXMLUWANT.XML
like this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Abrir"
android:title="#string/Abrir" />
<item android:id="#+id/Editar"
android:title="#string/Edit" />
<item android:id="#+id/Borrar"
android:title="#string/delete" />
<item android:id="#+id/Alarma"
android:title="#string/Alarma" />
</menu>
2º-AFTER in the class of u want do the context menu in LIST VIEW
lista = me listview
lista = (ListView) findViewById(R.id.Lista);
final ArrayAdapter<String> adaptador = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, ficheros);
lista.setLongClickable(true);
lista.setAdapter(adaptador);
lista.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) { //here u set u rute
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
}
});
3º AND FINALY, u set u onclick to items
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.Editar:
System.out.println("Editar");
return true;
case R.id.Borrar:
System.out.println("borrar");
return true;
case R.id.Abrir:
System.out.println("Abrir");
return true;
case R.id.Alarma:
System.out.println("Alarma");
return true;
default:
return super.onContextItemSelected(item);
}
}
for me all this WORKS :)

Related

How to open the ContextMenu from MenuItem Android

So, I know we need to pass a view to openContextMenu(view); but where can i get the view for Menuitem, please have a look at my code. Thanks in advance.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionmenu:
openContextMenu(item); //I dont know what to pass here
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, 0, 11, "Edit");
menu.add(0, 1, 12, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
System.out.println(item.getItemId());
return super.onContextItemSelected(item);
}
1. Register a view for a floating context menu
By default, a long-press on a view does not trigger the creation of a context menu.
You must register a view for a floating context menu by calling the following method,
a listview for example:
ListView listView = (ListView) v.findViewById(android.R.id.list);
registerForContextMenu(listView);
2. Create resource xml file
You'd better to create a xml resource file that contains the context menu item:
your_context.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/context_menu_eidt"
android:title="Edit" />
<item android:id="#+id/context_menu_delete"
android:title="Delete" />
</menu>
3. Inflate the resouce to build context menu
Then inflate the resource file in onCreateContextMenu method,
the parameter #v is the view that the context menu is being built for:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if (v.getId() == android.R.id.list) {
Log.d(TAG, "get the view here");
}
getActivity().getMenuInflater().inflate(R.menu.your_context, menu);
}
4. Do something when menu item selected
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
switch (item.getItemId()) {
case R.id.context_menu_eidt:
// TODO
return true;
case R.id.context_menu_delete:
// TODO
return true;
}
return super.onContextItemSelected(item);
}
That's all.

Using contextmenu with listview in android

I am developing an android application.I will have a listview and i have set a context menu to appear when a listview item is long-pressed.How do i get the item from the listview item selected(say text from a listview textview) after an action from the contextmenu is chosen so i can process it?
Here is some code:
protected void onCreate(Bundle savedInstanceState) {
-------
lv1 = (ListView) findViewById(R.id.listings);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(this);
}
And the onCreateContextMenu:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.watch:
String name = "";
return true;
case R.id.buy:
return true;
default:
return super.onContextItemSelected(item);
}
}
I want to get text from a textview in a list item.How do i achieve that?
To get the item from the ListView item selected refer to ContextMenuInfo object (see last implemented method below). Full solution as follows:
1) register ListView for context menu in ListActivity class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
getListView().setAdapter(mAdapter);
registerForContextMenu(getListView());
}
1a) if you have complex View on your list you might need to enable long click on each list view in Adapter class
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
itemLayout = layout;
itemLayout.setLongClickable(true);
}
// ...
return view;
}
2) implement onCreateContextMenu() and onContextItemSelected() in ListActivity class
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
String title = ((MyItem) mAdapter.getItem(info.position)).getTitle();
menu.setHeaderTitle(title);
menu.add(Menu.NONE, MENU_CONTEXT_DELETE_ID, Menu.NONE, DELETE_TEXT);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_CONTEXT_DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Log.d(TAG, "removing item pos=" + info.position);
mAdapter.remove(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
The problem was the onItemLongClick() method, do not use it for context menu.
Instead, register the LISTVIEW for the context menu.
Here's the source.
for onCreate():
registerForContextMenu(lv);
And to access the selected item during long click:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() == R.id.lv) {
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);
menu.add("One");
menu.add("Two");
menu.add("Three");
menu.add(obj.name);
}
}
1) First we use
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("View Selected Text");
}
2)
list--is ref if ListView
registerForContextMenu(list);
3)
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(item.getTitle().equals("View Selected Text"))
{
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
Contact c=array.get(menuInfo.position);
Toast.makeText(List.this, "Selected String is :-"+c.toString(), Toast.LENGTH_SHORT).show();
}
}
first get list using id
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
String packname = context.getPackageName();
Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
if(LaunchIntent != null){
startActivity(LaunchIntent);
}
else {
Toast.makeText(getActivity().getBaseContext(),"APPLICATION IN NOT AVAILABEL", Toast.LENGTH_SHORT).show();
}
Write this in your longPressListener with the listview you use:
ListView list = (ListView) findViewById(android.R.id.list);
registerForContextMenu(list);
And this are the methods:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
Adapter adapter = getListAdapter();
Object item = adapter.getItem(info.position);
menu.setHeaderTitle("Choose");
menu.add(0, v.getId(), 0, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Delete") {
deleteContact(item.getItemId());
} else if (...) {
// code
} else {
return false;
}
return true;
}
public void deleteContact(int id){
// your code what to do for the clicked item
}
use these methods, onCreateContextMenu and onContextItemSelected\
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.listview) {
menu.setHeaderTitle("Delete");
menu.add(Menu.NONE, 0, 0, "Delete from list");
}
}
/**
* Responding to context menu selected option
* */
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId();
// check for selected option
if (menuItemIndex == 0) {
// user selected delete
// delete the listrow
..(in your onitemclicklistener there is a parameter called as'postition' use this position and use some method to delete the data corresponding to the position value )
// reloading same activity again
Intent intent = getIntent();
finish();
startActivity(intent);
}
return true;
}
The above answers are very accurate and to the point for the case provided. That being said, I was brought here with using a convertView for my listview and am answering for those who are also brought here with this case.
If your LISTVIEW is using convertView and inflating a separate layout (say list_MyItem.xml), directly modify the list_MyItem.xml to have:
android:longClickable="true"
For example, if the listview is being populated with buttons modify the button as such:
<Button
android:id="#+id/myButton"
.
.
.
android:longClickable="true"
/>

android: listview long click & context popup causing stackoverflowerror (possibly due to actionbarsherlock?) [duplicate]

I am developing an android application.I will have a listview and i have set a context menu to appear when a listview item is long-pressed.How do i get the item from the listview item selected(say text from a listview textview) after an action from the contextmenu is chosen so i can process it?
Here is some code:
protected void onCreate(Bundle savedInstanceState) {
-------
lv1 = (ListView) findViewById(R.id.listings);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(this);
}
And the onCreateContextMenu:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.watch:
String name = "";
return true;
case R.id.buy:
return true;
default:
return super.onContextItemSelected(item);
}
}
I want to get text from a textview in a list item.How do i achieve that?
To get the item from the ListView item selected refer to ContextMenuInfo object (see last implemented method below). Full solution as follows:
1) register ListView for context menu in ListActivity class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
getListView().setAdapter(mAdapter);
registerForContextMenu(getListView());
}
1a) if you have complex View on your list you might need to enable long click on each list view in Adapter class
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
itemLayout = layout;
itemLayout.setLongClickable(true);
}
// ...
return view;
}
2) implement onCreateContextMenu() and onContextItemSelected() in ListActivity class
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
String title = ((MyItem) mAdapter.getItem(info.position)).getTitle();
menu.setHeaderTitle(title);
menu.add(Menu.NONE, MENU_CONTEXT_DELETE_ID, Menu.NONE, DELETE_TEXT);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_CONTEXT_DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Log.d(TAG, "removing item pos=" + info.position);
mAdapter.remove(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
The problem was the onItemLongClick() method, do not use it for context menu.
Instead, register the LISTVIEW for the context menu.
Here's the source.
for onCreate():
registerForContextMenu(lv);
And to access the selected item during long click:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() == R.id.lv) {
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);
menu.add("One");
menu.add("Two");
menu.add("Three");
menu.add(obj.name);
}
}
1) First we use
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("View Selected Text");
}
2)
list--is ref if ListView
registerForContextMenu(list);
3)
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(item.getTitle().equals("View Selected Text"))
{
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
Contact c=array.get(menuInfo.position);
Toast.makeText(List.this, "Selected String is :-"+c.toString(), Toast.LENGTH_SHORT).show();
}
}
first get list using id
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
String packname = context.getPackageName();
Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
if(LaunchIntent != null){
startActivity(LaunchIntent);
}
else {
Toast.makeText(getActivity().getBaseContext(),"APPLICATION IN NOT AVAILABEL", Toast.LENGTH_SHORT).show();
}
Write this in your longPressListener with the listview you use:
ListView list = (ListView) findViewById(android.R.id.list);
registerForContextMenu(list);
And this are the methods:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
Adapter adapter = getListAdapter();
Object item = adapter.getItem(info.position);
menu.setHeaderTitle("Choose");
menu.add(0, v.getId(), 0, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Delete") {
deleteContact(item.getItemId());
} else if (...) {
// code
} else {
return false;
}
return true;
}
public void deleteContact(int id){
// your code what to do for the clicked item
}
use these methods, onCreateContextMenu and onContextItemSelected\
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.listview) {
menu.setHeaderTitle("Delete");
menu.add(Menu.NONE, 0, 0, "Delete from list");
}
}
/**
* Responding to context menu selected option
* */
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId();
// check for selected option
if (menuItemIndex == 0) {
// user selected delete
// delete the listrow
..(in your onitemclicklistener there is a parameter called as'postition' use this position and use some method to delete the data corresponding to the position value )
// reloading same activity again
Intent intent = getIntent();
finish();
startActivity(intent);
}
return true;
}
The above answers are very accurate and to the point for the case provided. That being said, I was brought here with using a convertView for my listview and am answering for those who are also brought here with this case.
If your LISTVIEW is using convertView and inflating a separate layout (say list_MyItem.xml), directly modify the list_MyItem.xml to have:
android:longClickable="true"
For example, if the listview is being populated with buttons modify the button as such:
<Button
android:id="#+id/myButton"
.
.
.
android:longClickable="true"
/>

Android ListView can't click Item

I have a ListView. I implemented OnItemClickListener to open a ContextMenu when an item is clicked.
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
showMenu(view);
}
});
And the code to create menu.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.friend_list_menu, menu);
}
public void showMenu(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
My problem is, when I clicked on one item of ListView, it does open the Menu. But if I go back to the ListView, I can't click that item again. The same for other items, it can't be click after close the menu. Can anyone help me with this?
You've set that up incorrectly. You register for context menu when you set the adapter, not in a button click.
It should look like this:
setListAdapter(lists);
registerForContextMenu(getListView());
Then you have your onCreateContextMenu and onContextItemSelected methods (I create mine programatically, but your inflated one woudl work just as well):
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("List Operations");
menu.add(0, v.getId(), 0, "Edit List");
menu.add(0, v.getId(), 0, "Delete List");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
if (item.getTitle() == "Edit List") {
// Do your edit stuff here
} else if (item.getTitle() == "Delete List") {
// Do your delete stuff here
}
return super.onContextItemSelected(item);
}
You don't need to use onItemClick unless you want to do something on a short press of the item (context menu is long press).
The following code will works.
code:
ListView listview=(ListView)findViewByid(R.id.listview);
/**** here write appending data to listview*******/
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,/**your list items**/);
listview.setadapter(adp);
registerForContextMenu(listview);
//listview item click listener
listview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "selected" + p, 30).show();
}});
/****do your context menu code here*******/

How to change background color of each row in list view?

My app contains 1 list view, data source is 1 sqlite table, when i hold long click on any row in listview it will show me 1 menu option to change the color of that row, for this i have used onContextItemSelected function, on selecting menu option it will call 1 function change_color. What should i write in change_color function so that i can change row bg color.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color();
return true;
}
return super.onContextItemSelected(item);
}
Call your method as :
change_color(pass_your_list_view, pass_selected_position_of_list_view);
And define change_color() as:
private void change_color(ListView listView, int position) {
listView.getChildAt(position).setBackgroundColor(Color.BLACK);
}
Hope this will help.
Edited
Define a variable a position
public static int position;
And replace your code as
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
// Get the info on which item was selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// Retrieve the position at where you long pressed
position = info.position;
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color(getListView(), position);
return true;
}
return super.onContextItemSelected(item);
}
refer to this tutorial Colored Row in List View

Categories

Resources