Is it possible to add something to the list of items that shows up when a user long presses on any Edit Text? (Cut, copy paste, select text, select all, input method) I want to add another option to this menu, but cannot figure it out.
There is a duplicate of this question here, and the last comment for the first answer says it's 'possible, but not pretty'. Then the thread dies.
I'd really like to see any working example, dirty or not :)
Is it possible to add something to the list of items that shows up when a user long presses on any Edit Text?
If the EditText
is in your activity, you can do that via onCreateContextMenu()
.
If the EditText
is not in your activity, then no.
There is a duplicate of this question here, and the last comment for the first answer says it's 'possible, but not pretty'. Then the thread dies.
First, that post is two years old, which is nearly forever in Android years.
Second, I am fairly certain Mr. Haseman is incorrect in his assessment.
Adding some more menu items to the existing edittext context menu is only possible if the EditText is in your activity. This can be done via onCreateContextMenu()
.
If the EditText is not in your activity then its not possible.
// to add items to menu
EditText UserNameEditText = (EditText)findViewById(R.id.usernameEdittext);
registerForContextMenu(UserNameEditText);
// override the context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.usernameEdittext)
{
menu.add(0, 1, 0, "Fetch New Username");
menu.add(0, 2, 0, "Check For Duplicate");
}
}
If the context menu is not getting called then your edittext is not in your activity.
Both is Yes!
First ,you need to create a class which implementation OnCreateContextMenuListener,
public class CMenu implements OnCreateContextMenuListener {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
//Do Something , Like:
menu.add(0, 1, 0, "copy");
menu.add(0, 2, 0, "paste");
}
}
then
editText.setOnCreateContextMenuListener(cMenu);
It's Ok now~