EditText how to activate copy/paste popup without any actionbar?

loki picture loki · Sep 8, 2016 · Viewed 7.2k times · Source

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 have a popup with "select all/cut/copy/paste/etc" like you can see on the picture below.

However, when i select Theme.Material.Light.NoActionBar or Theme.Holo.Light.NoActionBar then i can't select any text in my EditText (i have no right or left text selection handles) and off course i don't have any copy/paste popup

Is their any way to have this copy/paste popup on Theme_Material_Light_NoActionBar ?

enter image description here Theme.DeviceDefault.Light.NoActionBar

enter image description here Theme_Material_Light_NoActionBar

enter image description here Theme.Holo.Light.NoActionBar

NOTE 1:

When i move the screen to the horizontal then the edittext take all the available space, and then i can see my right and left text selection handles like on the picture below, but i think it's because theme swap to Theme.DeviceDefault.Light.NoActionBar when i move the screen to the horizontal but i m not sure :

enter image description here

NOTE 2:

On my editText, when i do setCustomSelectionActionModeCallback(new Callback() {}) then the Callback is never called :( this is not normal i think ? What in the editText can forbid the callback ?

NOTE 2:

I can select text in all theme (but i can't copy it off course), but except in Theme.DeviceDefault.Light.NoActionBar i can't see the right and left text selection handle.

NOTE 3:

Theme.DeviceDefault.Light.NoActionBar show the right and left text selection handle only on some phones like the samsung galaxy. On some other it's didn't work.

NOTE 4:

i found partially the source of the problem! it's because i create my view via the WindowManager.addView(view, layout_params) and in this way startactionmodeforChild return null and this forbid the actionbar and the text selection handles to be show. Now if i do something like this in my edittext :

@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
    Activity host = (Activity) this.getContext();    
    return host.getWindow().getDecorView().startActionMode(callback); 
} 

then i can see the right and left text action handles (but not on marshmallow, it's work only on lollipop this i don't know why). My problem now is that the actionbar is showed, but it's showed empty :( nothing is draw inside (but i can see that the cut/copy/past control are inside in the dump view hierarchie). So now i m not looking for a way to replace this actionbar by a popup menu instead (like on the picture Theme.DeviceDefault.Light.NoActionBar). Any idea ?

Answer

W4R10CK picture W4R10CK · Sep 16, 2016

add following in your activity

ActionMode mActionMode;

and you have to create an ActionMondeCallback interface

class ActionBarCallback implements ActionMode.Callback
    {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(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);
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    }

where contextual_menu.xml is as follows with required icons

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns: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"
    >
    <item
        android:id="@+id/item_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="ifRoom|withText"
        android:title="Delete"
        android:titleCondensed="Delete">
    </item>
    <item
        android:id="@+id/item_delete"
        android:icon="@android:drawable/ic_menu_delete"
        app:showAsAction="ifRoom|withText"
        android:title="Delete"
        android:titleCondensed="Delete">
    </item>
    <item
        android: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(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mActionMode = MainActivity.this.startActionMode(new ActionBarCallback());
                return true;
            }
        });

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

~Bounty Hunter More details here