Skip to content Skip to sidebar Skip to footer

Edittext How To Activate Copy/paste Popup Without Any Actionbar?

I m under delphi and i use the android framework to create an edit. When i set the theme to Theme.DeviceDefault.Light.NoActionBar then i can select some text in my EditText and i h

Solution 1:

add following in your activity

ActionMode mActionMode;

and you have to create an ActionMondeCallback interface

classActionBarCallbackimplementsActionMode.Callback
    {

        @OverridepublicbooleanonCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
            returntrue;
        }

        @OverridepublicbooleanonPrepareActionMode(ActionMode mode, Menu menu) {
            returnfalse;
        }

        @OverridepublicbooleanonActionItemClicked(ActionMode mode, MenuItem item) {

            int id = item.getItemId();
            if(id == R.id.item_delete)
            {
                tv.setText("");
                Toast.makeText(MainActivity.this,"option deleted",Toast.LENGTH_LONG);
            }
            returnfalse;
        }

        @OverridepublicvoidonDestroyActionMode(ActionMode mode) {

        }
    }

where contextual_menu.xml is as follows with required icons

<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"tools:context="com.example.letschat"
    ><itemandroid:id="@+id/item_search"android:icon="@android:drawable/ic_menu_search"app:showAsAction="ifRoom|withText"android:title="Delete"android:titleCondensed="Delete"></item><itemandroid:id="@+id/item_delete"android:icon="@android:drawable/ic_menu_delete"app:showAsAction="ifRoom|withText"android:title="Delete"android:titleCondensed="Delete"></item><itemandroid:id="@+id/item_share"android:icon="@android:drawable/ic_menu_share"app:showAsAction="ifRoom|withText"android:title="Delete"android:titleCondensed="Delete"></item></menu>

Now Enable your Contextual ActionBar(CAB) As follows as for example here am are enabling on long click of a textview

yourtextView.setOnLongClickListener(newView.OnLongClickListener() {
            @OverridepublicbooleanonLongClick(View v) {
                mActionMode = MainActivity.this.startActionMode(newActionBarCallback());
                returntrue;
            }
        });

then you have to write your own action on click on each action event on CAB.

~Bounty HunterMore details here

Solution 2:

For API level 11 or above then you can stop copy,paste,cut and custom context menus from appearing by.

edittext.setCustomSelectionActionModeCallback(newActionMode.Callback() {

        publicbooleanonPrepareActionMode(ActionMode mode, Menu menu) {
            returnfalse;
        }

        publicvoidonDestroyActionMode(ActionMode mode) {                  
        }

        publicbooleanonCreateActionMode(ActionMode mode, Menu menu) {
            returnfalse;
        }

        publicbooleanonActionItemClicked(ActionMode mode, MenuItem item) {
            returnfalse;
        }
    });

Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).

Answer : Override isSuggestionsEnabled and canPaste in EditText.

For the quick solution, copy the class below - this class overrides the EditText class, and blocks all events accordingly.

For the gritty details, keep reading.

The solution lies in preventing PASTE/REPLACE menu from appearing in the show() method of the (non-documented) android.widget.Editor class. Before the menu appears, a check is done to if (!canPaste && !canSuggest) return;. The two methods that are used as the basis to set these variables are both in the EditText class:

isSuggestionsEnabled() is public, and may thus be overridden. canPaste() is not, and thus must be hidden by introducing a function of the same name in the derived class. So incorporating these updates into a class that also has the setCustomSelectionActionModeCallback, and the disabled long-click, here is the full class to prevent all editing (but still display the text selection handler) for controlling the cursor:

package com.cjbs.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;


/**
 *  This is a thin veneer over EditText, with copy/paste/spell-check removed.
 */publicclassNoMenuEditTextextendsEditText

{
    privatefinal Context context;

    /** This is a replacement method for the base TextView class' method of the same name. This 
     * method is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
     * appears when triggered from the text insertion handle. Returning false forces this window
     * to never appear.
     * @return false
     */booleancanPaste()
    {
       returnfalse;
    }

    /** This is a replacement method for the base TextView class' method of the same name. This method
     * is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
     * appears when triggered from the text insertion handle. Returning false forces this window
     * to never appear.
     * @return false
     */@OverridepublicbooleanisSuggestionsEnabled()
    {
        returnfalse;
    }

    publicNoMenuEditText(Context context)
    {
        super(context);
        this.context = context;
        init();
    }

    publicNoMenuEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.context = context;
        init();
    }

    publicNoMenuEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    privatevoidinit()
    {
        this.setCustomSelectionActionModeCallback(newActionModeCallbackInterceptor());
        this.setLongClickable(false);
    }


    /**
     * Prevents the action bar (top horizontal bar with cut, copy, paste, etc.) from appearing
     * by intercepting the callback that would cause it to be created, and returning false.
     */privateclassActionModeCallbackInterceptorimplementsActionMode.Callback
    {
        privatefinalStringTAG= NoMenuEditText.class.getSimpleName();

        publicbooleanonCreateActionMode(ActionMode mode, Menu menu) { returnfalse; }
        publicbooleanonPrepareActionMode(ActionMode mode, Menu menu) { returnfalse; }
        publicbooleanonActionItemClicked(ActionMode mode, MenuItem item) { returnfalse; }
        publicvoidonDestroyActionMode(ActionMode mode) {}
    }
} 

Solution 3:

Try to use theme Theme.AppCompat.Light.NoActionBar. Also set android:textIsSelectable="true" on your EditText in xml file.

Post a Comment for "Edittext How To Activate Copy/paste Popup Without Any Actionbar?"