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();
}
});
Related
Hi i made aplication(soundboard)
I have the buton when i click(OnClick) is plays music, when i hold button(longClick) is opens ContextMenu. In ContextMenu i have button. Now i need add the action for this button(button in ContextMenu) "share this music on messneger". Can help me someone?
Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button contextMenuButton = (Button) findViewById(R.id.smiech);
registerForContextMenu(contextMenuButton);
final MediaPlayer smiech = MediaPlayer.create(this, R.raw.smiech);
Button playsmiech = (Button) this.findViewById(R.id.smiech);
playsmiech.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
smiech.start();
}
});
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_main,menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_option1:
break;
case R.id.item_option2:
break;
case R.id.item_option3:
break;
}
return super.onContextItemSelected(item);
}
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 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;
}
});
}
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
I've a class (a view) that extends LinearLayout.
Now, I'm trying to do a context menu for each element of that class in my activity, but there's no way, it never fires:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
MyLinearLayout[] arrayLayout = new MyLinearLayout[num];
for (int i = 0; i < arrayLayout.length; i++)
{
//I ve removed code that fill the array and do some actions
//and then:
ll.addView(arrayLayout[i]);
registerForContextMenu(arrayLayout[i]);
}
setContentView(ll);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Log.i(LOGTAG, "context menu");
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, R.string.CTX_EDIT);
menu.add(0, v.getId(), 0, R.string.CTX_ELIM);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
function1(item.getItemId());
return true;
}
public void function1(int id){
Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
}
The context menu never shows, i've read a lot problems about it, and i think this is because of the LinearLayout extended class, but I'm sure there is a way to do this, or i'm missing something. Can anyone help, please? thanks!
You are creating an empty array of MyLinearLayout: arrayLayout = new MyLinearLayout[num],
Then you are trying to set a context menu with:
registerForContextMenu(arrayLayout[i])
Which does nothing since arrayLayout[i] is null.