Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
You are not comparing Strings properly. This test:
if(item.getTitle()=="Ringtone")
checks if the object returned by getTitle() is the same object as "Ringtone". This will always be false. Similarly with the else if test. You actually want to compare the value of getTitle with your String. You should replace them with the String#equals() method. Here is the code:
if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}
else {return false;}
If you are using a ListFragment then you also need to do
getListView().setOnCreateContextMenuListener(this);
instead of
registerForContextMenu(listView);
it seems.
Related
I'm trying to make a toast on item selected but it's showing unreachable statement error.
My code is:
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.performLongClick();
}
}
);
registerForContextMenu(btn);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("For Optimum Results");
menu.add(0,v.getId(),0,"Hi");
menu.add(0,v.getId(),0,"Hello");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if(item.getTitle()=="Hi"){
Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
}
return true;
}
you have two type of error:
1)return super (return super.onContextItemSelected(item)) , it means that you return your method at first line so lines below not execute.
2)wrong comparing String (item.getTitle()=="Hi")
correct code should be like this:
#Override
public boolean onContextItemSelected(MenuItem item) {
//return super.onContextItemSelected(item);//remove this line
if(item.getTitle().equal("Hi")){// also maybe you want to check not null for item.getTitle()
Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
}
return true;
}
Dialog dialog;
private void opendialog() {
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.popup);
dialog.setTitle(R.string.msettings);
RelativeLayout reply_layout = (RelativeLayout) dialog
.findViewById(R.id.reply_layout);
final RelativeLayout ringtone_layout = (RelativeLayout) dialog
.findViewById(R.id.ringtone_layout);
registerForContextMenu(ringtone_layout);
ringtone_layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(ringtone_layout);
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
System.out.println("Inside onContextItemSelected");
return super.onContextItemSelected(item);
}
onContextItemSelected is never called when use context menu inside a Dialog. Is there any thing wrong with my code ? Thanks in advance..
NOTE: Since this answer seems to be getting some attention (upvotes), I am editing the code snippet to reflect a more concise answer to the question.
You are trying to register for the context menu for a view item within the dialog but from the activity. This approach is wrong. You actually need to subclass Dialog and then create and expand your views there and then override the onCreateContextMenu() there to do your work and register your view for the context menu. You should then create an instance of that dialog here. So it will be something like:
public class Mydialog extends Dialog {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
dialog.setTitle(R.string.msettings);
RelativeLayout reply_layout = (RelativeLayout) findViewById(R.id.reply_layout);
final RelativeLayout ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);
registerForContextMenu(ringtone_layout);
ringtone_layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(ringtone_layout);
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
}
// You should do the processing for the selected context item here. The
// selected context item gets passed in the MenuItem parameter in
// the following method. In my answer I am force calling the onContextItemSelected()
// method but you are free to do the actual processing here itself
#Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
return onContextItemSelected(aMenuItem);
else
return super.onMenuItemSelected(aFeatureId, aMenuItem);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// Avoid using System.out.println() - instead use Android Logging
return super.onContextItemSelected(item);
}
}
Now, you can create an instance of this dialog and your views will have the context item registered successfully. So your openDialogMethod() will now look like:
private void opendialog() {
dialog = new MyDialog(MainActivity.this);
// the context menu will now be registered
dialog.show();
}
Although you were originally passing the context of the activity to the Dialog that you were creating, you cannot pass on the context menu creation listeners like that. To do that you will have to subclass your dialog as I have shown above.
EDIT: After looking at Zsolt's answer, I found out that I overlooked this line of code that you did not have. You need to have:
ringtone_layout.setOnCreateContextMenuListener(this);
What you say about forcefully calling the context menu is actually true. You are just calling the regular menu and then you are passing that id on to the context menu callback. The if clause passes because the id is from the context menu feature.
And after some further reading, it looks like you are supposed to do your menu handling on onMenuItemSelected() and not in onContextItemSelected(). So what you have now is correct and you do not need to forcefully call the other method. Just do your processing in onMenuItemSelected().
This issue already solved in the following SO threads:
onContextItemSelected doesn't get called and onContextItemSelected never called using a Dialog with a ListView.
If none of the answers helped you, try this:
return false in onContextItemSelected method in your activity and in its fragments.
public class MusicsetDialog extends Dialog implements OnClickListener {
private RelativeLayout ringtone_layout;
public MusicsetDialog(Context context) {
super(context);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_popup);
setTitle(R.string.msettings);
ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);
ringtone_layout.setOnClickListener(MusicsetDialog.this);
registerForContextMenu(ringtone_layout);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ringtone_layout:
openContextMenu(ringtone_layout);
break;
default:
break;
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select The Action");
// groupId, itemId, order,title
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
return onContextItemSelected(aMenuItem);
else
return super.onMenuItemSelected(aFeatureId, aMenuItem);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// System.out.println("onContextItemSelected");
Log.d("MusicsetDialog", "onContextItemSelected");
return super.onContextItemSelected(item);
}
}
I have created a subclass for mydialog and extended Dialog and followed the procedure of Sunil. But still onContextItemSelected was not called. so after doing some research, I Override onMenuItemSelected and passed the selected MenuItem to onContextItemSelected and it worked fine. may be i'm calling onContextItemSelected forcefully. I don't know. I'm new to android, any explanation why onContextItemSelected is still not called automatically would be appreciated .. Thanks..
Try this
new AlertDialog.Builder(this)
.setSingleChoiceItems(items, 0, null)
.setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
// Do something useful withe the position of the selected radio button
}
})
.show();
I use this code for my contextual menu and I make a simple commands. But how to integrate real commands in this?
I want make real function.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Optiuni");
menu.add(0, v.getId(), 0, "Copiaza");
menu.add(0, v.getId(), 0, "Sterge");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Copiaza"){copyFunction(item.getItemId());}
else if(item.getTitle()=="Sterge"){deleteFunction(item.getItemId());}
else {return false;}
return true;
}
public void deleteFunction(int id){
Toast.makeText(this, "Sters", Toast.LENGTH_SHORT).show();
}
public void copyFunction(int id){
Toast.makeText(this, "Copiat", Toast.LENGTH_SHORT).show();
}
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);
}
});
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.