Skip to content Skip to sidebar Skip to footer

Android Long Press On Edit Text Behavior

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

Solution 1:

Thats not possible, as the context menu is populated by applications themselves and not by the system. You cannot force other apps to have a context item that they might not use in their lifetime. You can atleast have the feature in apps that are aware of your app. Create an activity that populates and handles only your global menu items. Other apps can use the feature by extending your activity. But this too will create problems, as the other apps will have a hard dependency on your app. So if your app is not installed in that system then the other app won't work. Also there is no way to indicate of this dependency in the manifest file so that the dependent app is hidden in the market if your app is not already installed. I'm sure this is not the answer you were looking for, but context menus are made so by design.

Solution 2:

There are 2 ways: 1st one described by Shahab. 2nd one is more simple. You need to just override standard method of your activity, like:

@OverridepublicvoidonCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
{
       if(view.getId()==R.id.MyEditTextId)
       {
            menu.add(Menu.NONE, MyMenu, Menu.NONE, R.string.MyMenuText);
       }
       elsesuper.onCreateContextMenu(menu, view, menuInfo);
}

After that you'll have on long press popup context menu

Solution 3:

Yes it is possible to add something to the list of items on LongClick of EditText.

To put you in right direction I am posting some code snippets.

In main.xml do something like

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
><EditTextandroid:id="@+id/textt"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"
/></LinearLayout>

After that in your main Activity, do something like

publicclasseditextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditTexttext= (EditText)this.findViewById(R.id.textt);
    text.setOnLongClickListener(newOnLongClickListener() {

        @OverridepublicbooleanonLongClick(View v) {

            //ADD HERE ABOUT CUT COPY PASTE  // TODO Auto-generated method stubreturnfalse;
        }
    });
}
}

Hope it helps

Post a Comment for "Android Long Press On Edit Text Behavior"