i have listview the fisrt time i when i click to row it open the context menu then i override the oncontextitemselected
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case CALL_ID:
{
AdapterContextMenuInfo info2 = (AdapterContextMenuInfo) item.getMenuInfo();
String phone=mDbHelper.getPhone(info2.id);
String toDial="tel:"+phone.toString();
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse(toDial)));
return true;
}
}
return super.onContextItemSelected(item);
}
this do correctly but when i
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case CALL_ID:
{
AdapterContextMenuInfo info2 = (AdapterContextMenuInfo) item.getMenuInfo();
String phone=mDbHelper.getPhone(info2.id);
String toDial="tel:"+phone.toString();
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse(toDial)));
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
the app crashed can u show me the difference between them
Try this:
context_menu.xml (res/menu/context_menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/call"
android:title="CALL" />
</menu>
Context Menu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
info = (AdapterView.AdapterContextMenuInfo)menuInfo;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
String phone="555-555-555";
String toDial="tel:"+phone.toString();
Uri uri = Uri.parse(toDial);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
return true;
default:
return super.onContextItemSelected(item);
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
That should work.
Related
I am using a ViewPager. I have two tabs and, consequently, two fragments that each one have their own Context Menu Options. My problem is when I click in Context Menu options in Fragment B, the method onContextItemSelected() in fragment A is called. How can I solve this?
I do the same as shown in the official Android documentation:
https://developer.android.com/guide/topics/ui/menus.html#context-menu
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
PositionClicked = info.position;
switch (item.getItemId()) {
case R.id.menu_edit:
return true;
case R.id.menu_archive:
return true;
case R.id.menu_report:
return true;
case R.id.menu_delete:
Log.i(TAG, "Delete Menu Buttom");
new AlertDialog.Builder(getActivity()).setTitle("Tem certeza?")
.setMessage("Você está prestes a deletar sua Postagem!")
.setPositiveButton("Deletar", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//ShowSwipeRefreshin();
delete_item(MongodbObjtoDelete);
DialogResp = true;
feedItems.remove(PositionClicked);
listAdapter.notifyDataSetChanged();
}
}).setNegativeButton("Cancelar", null).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
you can fix it like this :
FragmentManager manager = getFragmentManager();
if (manager.findFragmentByTag("frag1").isVisible()){
// do fragment 1
}else{
}
Example of working ContextMenu
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle("");
menu.setHeaderIcon("");
String[] menuItems = getResources().getStringArray(R.array.ContextMenu);
for (int I = 0; I < menuItems.length; I++) {
menu.add(Menu.NONE, I, I, menuItems[I]);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
if(menuItemIndex == 0){
//do something
}
return true;
}
also check which views registered for Context menu.
registerForContextMenu(view);
i am trying to make my webview bring an option to save the current image when the image is long tapped i have tried the below code outside the oncreate but it does not do anything at all!
public boolean onLongClick(View v) {
openContextMenu(v);
return true;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.save_image:
Toast.makeText(this, "save failed",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
#Override
protected void onCreateContextMenu(ContextMenu menu) {
super.onCreateContextMenu(menu);
HitTestResult result = getHitTestResult();
MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// do the menu action
return true;
}
};
if (result.getType() == HitTestResult.IMAGE_TYPE ||
result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
// Menu options for an image.
//set the header title to the image url
menu.setHeaderTitle(result.getExtra());
menu.add(0, ID_SAVEIMAGE, 0, "Save Image").setOnMenuItemClickListener(handler);
menu.add(0, ID_VIEWIMAGE, 0, "View Image").setOnMenuItemClickListener(handler);
}
}
Here is the preparation code:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.language_menu, menu);
MenuItem menuItem = menu.findItem(R.id.arabic);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageArabic) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
menuItem = menu.findItem(R.id.english);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageEnglish) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.arabic:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageArabic) UtilityPreferenceManager.changeLanguage();
return true;
case R.id.english:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageEnglish) UtilityPreferenceManager.changeLanguage();
return true;
default:
return super.onContextItemSelected(item);
}
}
and here is the menu xml:
<group android:checkableBehavior="single">
<item android:id="#+id/arabic"
android:title="#string/arabic" />
<item android:id="#+id/english"
android:title="#string/english" />
</group>
The problem is that the menu always appears with English selected. I am sure that the language preference is saved correctly in preferences. In fact, the if condition is working fine, but it seems there is something overriding the selected menu item after finishing onCreateContextMenu
This is the onCreate and oncontextitemslected code
#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);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
Toast toast;
if(item.getItemId() == R.id.context_menu_edit)
{
Log.d("ContextCheck","EDIT!");
toast = Toast.makeText(this, "Edit!", Toast.LENGTH_SHORT);
toast.show();
}
if(item.getItemId() == R.id.context_menu_delete)
{
Log.d("ContextCheck","DELETE!");
toast = Toast.makeText(this, "Delete!", Toast.LENGTH_SHORT);
toast.show();
}
return super.onContextItemSelected(item);
}
and before that is i used the method registerForContextMenu(event_list) where event_list is a ListView , no i don't know why when ever i click an item from the context menu, it doesn't do anything, it won't show the toast and won't log into the logcat... is the item.getItemId() same for OptionsMenu and ContextManu?.. i don't know what is wrong with my code..
PS
the context menu is called inside a dialog box in a listview
Here is your solution, if you don't mind creating the menu items in your class. The keyword was definitely your PS, meaning your listview is in a dialog.
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.context_menu, menu);
MenuItem delete = menu.add("delete");
MenuItem add = menu.add("add");
add.setIcon(android.R.drawable.ic_menu_upload); //adding icons
delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
add.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
}
You do not even need the onContextItemSelected method.
You need to return true in the onCreateOptionsMenu as detailed in the documentation:
Returns
You must return true for the menu to be displayed; if you
return false it will not be shown.
So you can chacnge your code to this:
#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);
return true;
}
UPDATE:
I have things return on the options menu with a switch case in a onOptionsItemSelected vs onContextItemSelected
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.emaildev:
email();
break;
case R.id.share:
Share();
break;
}
return true;
}
The icons and ids are in my menu.xml
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);
}
});