Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add options to Android's native 'Edit text' context menu

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 :)

like image 208
suomi35 Avatar asked Feb 03 '11 23:02

suomi35


People also ask

How do I change the style of edit text in Android Studio?

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.

Where is context menu in Android?

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.

What types of menus does Android support?

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.


2 Answers

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.

like image 96
Srihari Avatar answered Oct 25 '22 02:10

Srihari


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~

like image 42
Young Avatar answered Oct 25 '22 04:10

Young