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 :)
You can use the attribute style="@style/your_style" that is defined for any widget. The attribute parent="@android:style/Widget. EditText" is important because it will ensure that the style being defined extends the basic Android EditText style, thus only properties different from the default style need to be defined.
A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.
There are three types of menus in Android: Popup, Contextual and Options. Each one has a specific use case and code that goes along with it. To learn how to use them, read on.
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~
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With